寫在前面
- 在手機APP日益增加的前提下,如何更好的提升用戶的交互體驗似乎成為衡量一個APP重要指標。上述的感悟源于實際工作的需求,就是在APP中添加一個更換用戶頭像的功能。
- 也許別人會認為這樣一個小功能不算什么,但從用戶交互角度考慮,這樣一個功能的設計有一定學問,待我慢慢道來。
獲取相冊最直接的方式——UIImagePickerController
功能介紹:可直接顯示分組的相處的列表,用戶選擇不同相冊的照片后,可在委托方法中獲得該圖片對象;
API提供三種數據源:
UIImagePickerControllerSourceTypeCamera: //拍照
UIImagePickerControllerSourceTypePhotoLibrary: //相冊
UIImagePickerControllerSourceTypeSavedPhotosAlbum: //圖片庫
- 基本使用
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
else imagePicker.
delegate =
self;
寫到這里,基本的調用系統相冊的功能就實現了,唯一需要做的是參數配置- 遵守的協議
**UINavigationControllerDelegate**,**UIImagePickerControllerDelegate**代理方法- (
void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(
NSDictionary<
NSString *,
id> *)info{}- (
void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{}- 捕捉多媒體的的類型 UIImagePickerControllerCameraCaptureModeUIImagePickerControllerCameraCaptureModePhoto,UIImagePickerControllerCameraCaptureModeVideo- 攝像頭的類型 UIImagePickerControllerCameraDeviceUIImagePickerControllerCameraDeviceRear,UIImagePickerControllerCameraDeviceFront
- 設置閃光燈的模式 UIImagePickerControllerCameraFlashMode UIImagePickerControllerCameraFlashModeOff = -
1,UIImagePickerControllerCameraFlashModeAuto =
0,UIImagePickerControllerCameraFlashModeOn =
1####自定義相冊方式之一 ALAssetsibrary- 基本介紹:該框架可實現自定義相冊,實現定制的圖片選擇器,可支持多選、自定義界面,只不過API在iOS9
.0版本被標記廢棄,即iOS9
.0之前的版本可以使用ALAssetsLibrary實現自定義,iOS9
.0之后的版本需要使用Photos
.fraework。- 成員介紹:
1.ALAssetsGroup:映射照片庫(ALAssetsLibrary)中的一個相冊,通過ALAssetsGroup可以獲取相冊相應的信息,以及獲取到對應相冊下的所有圖片資源;
2.ALAsset:對應相冊中的一張圖片或者一個視頻,并且包含對應圖片和視頻的詳細信息,可獲取圖片對應的縮略圖,還可通過ALAsset的實例方法保存圖片和視頻;
3.ALAssetRepresentation:可簡單理解為對ALAsset的封裝,對于給定的ALAsset都至少會對應一個ALAssetRepresentation,通過ALAsset的實例方法defaultRepresentation獲得對應的ALAssetRepresentation,例如使用系統相機的拍攝的RAW+JPEG照片,則會有兩個ALAssetRepresentation,一個封裝了RAW信息,另一個封裝了JPEG的信息。通過ALAssetRepresentation可以獲取ALAsset的原圖、全屏圖、文件名等信息;- 自定義行相冊的思路
1.實例化照片庫,獲取所有的相冊;
2.展示相冊中的所有照片,可自義展示樣式,多以集合視圖的形式展現;
3.選擇照片后返回上級界面或者進入預覽圖。- 具體實現
1.導入頭文件**
#import <AssetsLibrary/ALAssetsLibrary.h>** 或者 ** @import AssetsLibrary;**2.實例化AssetsLibrary**ALAssetsLibrary *assertLibray = [[ALAssetsLibrary alloc]init];**
3.**遍歷照片庫所有的相冊**groups = [
NSMutableArray array];[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group,
BOOL *stop) {
if (group) {[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group
.numberOfAssets) {[groups addObject:group];}}
else{
if (groups
.count) {[
self enumenumerateAssets];}
else{
NSLog(@
"no group");}}} failureBlock:^(
NSError *error) {
if (error) {
NSLog(@
"error = %@", [error description]);}}];
4.**遍歷相冊中的照片**- (
void)enumerateAssets{
NSMutableArray *assetArray = [
NSMutableArray new];
for (ALAssetsGroup *group in groups) {[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index,
BOOL *stop) {
if (result) {[assetArray addObject:result];}
else{
NSLog(@
"here is no photos");}}];
NSInteger fromIndex =
0;
NSInteger toIndex =
5;[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:toIndex] options:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index,
BOOL *stop) {
if (index > toIndex) {*stop =
YES;}
else{
if (result) {[assetArray addObject:result];}
else{
NSLog(@
"enumeration has ended!");}}}];}}
5 **完成上述步驟后,就能獲得所有相冊和相冊中對應的所有照片,接下來就可以根據自己的需求自定義顯示界面了,這里就不再一一贅述了。**
####自定義相冊方式之二Photos.framework- 基本介紹:Photos是蘋果在iOS8
.0提出的API,是目前,蘋果推薦的照片框架,學習一下還是很有必要的;
- 主要成員介紹:
1.PHAsset:代表照片庫中的一個資源,與ALAsset類似,通過PHAsset可以獲取和保存資源;
2.PHFetchOptions:獲取資源時的參數;
3.PHAssetCollection:PHCollection的子類,表示一個相冊或者一個時刻,也可以是一個【智能相冊】(系統提供的一系列相冊集合,包括最近刪除、相機相冊、最愛相冊等等)中的一個;
4.PHFetchResult:表示一系列資源結果的集合,也可以是相冊資源集合,一般情況下,可以從PHCollection或PHAsset的類方法中獲取;
5.PHImageManager:用于處理資源的加載,圖片加載的過程帶有緩存處理;
6.PHImageRequestOptions:控制加載資源的時一系列參數。- 具體使用
1.導入框架**@import Photos;**
2.**獲取系統相冊,系統提供下列三種獲取不同分類相冊的方法。**``` PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:
nil];PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:
nil];PHFetchOptions *allPhotoOptions = [[PHFetchOptions alloc] init];allPhotoOptions
.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@
"creationDate" ascending:
YES]];PHFetchResult *allphotos = [PHAsset fetchAssetsWithOptions:allPhotoOptions];```
3 獲取對應的照片資源```PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:
nil];
for (
NSInteger i =
0; i < smartAlbums
.count; i ++) {PHCollection *collection = smartAlbums[i];
if ([collection isKindOfClass:[PHAssetCollection class]]) {PHAssetCollection *assetCollection = (PHAssetCollection *)collection;PHFetchResult *photoSet = [PHAsset fetchAssetsInAssetCollection:assetCollection options:
nil];
for (
NSInteger j =
0; j < photoSet
.count; i ++) {PHAsset *asset = photoSet[i];} }
else{
NSLog(@
"not PHAssetCollection");}}PHFetchOptions *allPhotoOptions = [[PHFetchOptions alloc] init];allPhotoOptions
.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@
"creationDate" ascending:
YES]];PHFetchResult *allphotos = [PHAsset fetchAssetsWithOptions:allPhotoOptions];
for (
NSInteger i =
0; i < allphotos
.count ; i ++) {PHAsset *asset = allphotos[i];}
4 顯示圖片資源,需要用到PHImageManager類
```PHImageManager *imageManager = [[PHImageManager alloc]init];//獲取第一張照片資源PHAsset *asset = allphotos[0];//設定顯示照片的尺寸CGFloat width = _showImageView.frame.size.width;CGFloat height = _showImageView.frame.size.height;CGSize imageSize = CGSizeMake(width, height);[imageManager requestImageForAsset:assettargetSize:imageSizecontentMode:PHImageContentModeAspectFilloptions:nilresultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {//顯示照片_showImageView.image = result;}];
“`
ALAssetsibrary與Photos的對比
- 適用的iOS版本不同,ALAssetsibrary適用于iOS9.0之前,Photos適用于iOS9.0之后;
- 獲取資源的方式不同:**ALAssetsibrary都是以枚舉的方式獲取資源的,遍歷照片庫(ALAssetsibrary)獲得相冊(ALAssetsGroup),通過遍歷相冊獲得具體資源(ALAsset),枚舉方式獲取資源,存在效率低且不靈活的缺點;Photos采用拉取的方式獲取資源,由上述方法可知,多使用**PHFetchResult獲取對應資源,不采用枚舉方式獲取資源,在效率上會有所提高;
- 以上內容,就是我對常用算法的簡單總結。大家如有其他意見歡迎評論。
- 掃一掃下面的二維碼,歡迎關注我的個人微信公眾號攻城獅的動態(ID:iOSDevSkills),可在微信公眾號進行留言,更多精彩技術文章,期待您的加入!一起討論,一起成長!一起攻城獅!
總結
以上是生活随笔為你收集整理的iOS中,系统相册的那些事的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。