Apple 关于Photo Library

2017-11-17  本文已影响0人  AroundWind
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //设置获取源
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@", info);
}

其中获取源类型有以下几种:

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
  //相薄模式
    UIImagePickerControllerSourceTypePhotoLibrary,
  //相机模式
    UIImagePickerControllerSourceTypeCamera,
  //时刻照片模式
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
} __TVOS_PROHIBITED;

运行后得到的打印结果:

2017-11-17 12:34:09.337 PhotoOperation[646:10725] {
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x7a2b1120> size {4288, 2848} orientation 0 scale 1.000000";
    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=106E99A1-4F6A-45A2-B320-B0AD4A8E8473&ext=JPG";
}

注意:
1.遵守<UINavigationControllerDelegate,UIImagePickerControllerDelegate>协议
2.在 info.plist 文件中添加Privacy - Photo Library Usage Description 描述

然而,获取单张照片并不能满足用户的需求了,毕竟在要求多张的情况下,一张一张取会降低用户体验。

/**
 获得相机胶卷的所有图片
 */
- (void)getImagesFromCameraRoll{
    PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsWithOptions:nil];
    
    for (PHAsset *asset in assets) {
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            NSLog(@"%@", result);
        }];
    }
}

得到的打印结果:

2017-11-17 13:07:14.817 PhotoOperation[711:20042] <UIImage: 0x79fbde30>, {60, 40}
2017-11-17 13:07:14.823 PhotoOperation[711:20042] <UIImage: 0x79fbce60>, {60, 40}
2017-11-17 13:07:14.829 PhotoOperation[711:20042] <UIImage: 0x79e524a0>, {60, 40}
2017-11-17 13:07:14.838 PhotoOperation[711:20042] <UIImage: 0x79e65ce0>, {40, 60}
2017-11-17 13:07:14.844 PhotoOperation[711:20042] <UIImage: 0x79e53b00>, {60, 40}
2017-11-17 13:07:16.581 PhotoOperation[711:20042] <UIImage: 0x79fc3570>, {1668, 2500}
2017-11-17 13:07:16.808 PhotoOperation[711:20042] <UIImage: 0x79e6c380>, {3000, 2002}
2017-11-17 13:07:16.938 PhotoOperation[711:20042] <UIImage: 0x79fc6120>, {3000, 2002}
2017-11-17 13:07:17.177 PhotoOperation[711:20042] <UIImage: 0x79e756b0>, {4288, 2848}
2017-11-17 13:07:17.236 PhotoOperation[711:20042] <UIImage: 0x79e6acc0>, {4288, 2848}

注意:前一半是缩略图,后一半是原图,即显示照片数量应该为返回数量的一半。

虽然这样能获取多张图片了,但是如果图片很多的话,用户还是不好选,我们应该考虑先获取相薄,让用户选择相薄后,再选择照片则会容易很多。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取应用自己创建的相薄
    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (PHAssetCollection *assetCollection in assetCollections) {
        NSLog(@"1--%@", assetCollection.localizedTitle);
    }
   
    //获取系统的相机胶卷
     PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
}
/**
 获取某个相薄中的所有图片
 @param assetCollection 相薄
 @param original 是否原图
 */
- (void)enumerateAllAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original{
    //同步获得图片,只会返回1张图片
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = YES;
    
    //获取某个相薄中的所有PHAsset对象
    PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
    for (PHAsset *asset in assets) {
        //是否要原图
        CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero;
        //从asset中获取图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            NSLog(@"%@", result);
        }];
    }
}

最后,自己弄个 tableView 和 collectionView 就可以自定义多选图控件了。

上一篇下一篇

猜你喜欢

热点阅读