photos

1.查询相册使用权限
PHAuthorizationStatus st = [PHPhotoLibrary authorizationStatus];
if (PHAuthorizationStatusDenied == st || PHAuthorizationStatusRestricted == st) {
NSLog(@"相册不允许访问");
}
if (PHAuthorizationStatusAuthorized == st || PHAuthorizationStatusNotDetermined == st) {
NSLog(@"相册允许访问or用户未选择");
}

//相册变更
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
//回调方法如下,不过这个地方是有一个坑的,就是这个回调是在一个子线程里面,如果你想要对UI进行操作的话,则必须回调主线程
//相册变化回调

  • (void)photoLibraryDidChange:(PHChange *)changeInstance {
    dispatch_sync(dispatch_get_main_queue(), ^{
    // your codes
    });
    }

2.创建自己的相册

[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"相册名字"];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
}];

3.获取自己创建的相册

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
PHAssetCollection *collection = nil;
for (PHAssetCollection *accc in userAlbums) {
if ([accc.localizedTitle isEqualToString:@"相册名字"]) {
collection = accc;
}
}

4.保存图片到自己相册 —-删除指定相片类似

  • (void)addCustomAsset:(UIImage *)image
    collection:(PHAssetCollection *)collection
    completionHandler:(void (^)(void))successBlock
    failture:(void (^)(NSString * _Nonnull))failtureBlock
    {
    //执行变化请求
    [self.photoLibaray performChanges:^{
    //如果相册允许操作
    if([collection canPerformEditOperation:PHCollectionEditOperationAddContent]){
    //创建资源请求对象
    PHAssetChangeRequest * assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    //创建相册请求对象
    PHAssetCollectionChangeRequest * groupChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
    //向相册中添加资源。要删除相片,换个方法即可
    [groupChangeRequest addAssets:@[assetChangeRequest.placeholderForCreatedAsset]];
    }
    }completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (success == YES)//成功 {
    successBlock();return;
    }
    //失败
    failtureBlock(error.localizedDescription);
    }];
    }

5.保存视频到自己相册

  • (void)addCustomAsset2:(NSURL *)url
    collection:(PHAssetCollection *)collection
    completionHandler:(void (^)(void))successBlock
    failture:(void (^)(NSString * _Nonnull))failtureBlock {
    //执行变化请求
    [self.photoLibaray performChanges:^{
    //如果相册允许操作
    if([collection canPerformEditOperation:PHCollectionEditOperationAddContent]){
    //创建资源请求对象
    PHAssetChangeRequest * assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
    //创建相册请求对象
    PHAssetCollectionChangeRequest * groupChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
    //向相册中添加资源
    [groupChangeRequest addAssets:@[assetChangeRequest.placeholderForCreatedAsset]];
    }
    }completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (success == true)//成功 {
    successBlock();return;
    }
    //失败
    failtureBlock(error.localizedDescription);
    }];
    }

6.保存图片/视频到相机胶卷

UISaveVideoAtPathToSavedPhotosAlbum(self.videoPath, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

//图片保存回调

  • (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
    NSLog(@"保存成功与否提示");
    }

//录像保存完毕

  • (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo {
    NSLog(@"保存成功与否提示");
    }

7.获取自己相册的图片

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
//按创建日期排序,最新在前面
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//筛选图片,只要图片。只要视频mediaType=2
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType=1"];
//collection 可以从前面方法获取到
PHFetchResult *albumsPic = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
//获取第二张图
PHAsset *asset = albumsPic[1];
PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
//可以设置options,获取到图片的质量
// PHImageRequestOptions *options = [PHImageRequestOptions new];
// options.version = PHImageRequestOptionsVersionCurrent;
// options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
// options.resizeMode = PHImageRequestOptionsResizeModeFast;
[imageManager requestImageForAsset:asset
targetSize:AssetGridThumbnailSize
contentMode:PHImageContentModeDefault
options:nil//options
resultHandler:^(UIImage *result, NSDictionary *info) {
self.imageView.image = result;
}];

8.获取胶卷所以图片/视频/音频

PHFetchOptions *options = [[PHFetchOptions alloc] init];
/// 按创建日期排序
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//获取胶卷所有图片。要获取视频,把类型改为PHAssetMediaTypeVideo,音频的为PHAssetMediaTypeAudio
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:options];

//枚举里面的内容
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
}];

9.只能相册获取

//获取所有智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
NSLog(@"相册名字:%@", collection.localizedTitle);
}];

//智能相册的title不出意外都是英文,下面提供一个对应的中文名

  • (NSString *)transformAblumTitle:(NSString *)title
    {
    if ([title isEqualToString:@"Slo-mo"]) {
    return @"慢动作";
    } else if ([title isEqualToString:@"Recently Added"]) {
    return @"最近添加";
    } else if ([title isEqualToString:@"Favorites"]) {
    return @"最爱";
    } else if ([title isEqualToString:@"Recently Deleted"]) {
    return @"最近删除";
    } else if ([title isEqualToString:@"Videos"]) {
    return @"视频";
    } else if ([title isEqualToString:@"All Photos"]) {
    return @"所有照片";
    } else if ([title isEqualToString:@"Selfies"]) {
    return @"自拍";
    } else if ([title isEqualToString:@"Screenshots"]) {
    return @"屏幕快照";
    } else if ([title isEqualToString:@"Camera Roll"]) {
    return @"相机胶卷";
    }
    return nil;
    }

你可能感兴趣的:(photos)