[x-Code7新功能之三]PHPhotoLibrary的练习

#pragma mark - 相册的代理
-(void)photoLibraryDidChange:(PHChange *)changeInstance
{
    dispatch_async(dispatch_get_main_queue(), ^{
        PHObjectChangeDetails* isChange = [changeInstance changeDetailsForObject:_SelectCollection];
        if (isChange) {
            _AllAssets = [PHAsset fetchAssetsInAssetCollection:_SelectCollection options:nil];
            [self.collectionView reloadData];
        }
    });
}

#pragma mark - 拍照处理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    //判断是静态图像还是视频
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //相册是否支持添加图片
        if ([_SelectCollection canPerformEditOperation:PHCollectionEditOperationAddContent]) {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                //获取用户编辑之后的图像
                UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
                PHAssetChangeRequest* request = [PHAssetChangeRequest creationRequestForAssetFromImage:editedImage];
                PHAssetCollectionChangeRequest *CollentionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:_SelectCollection];
                PHObjectPlaceholder* holder = [request placeholderForCreatedAsset];
                [CollentionRequest addAssets:@[holder]];
            } completionHandler:^(BOOL success, NSError * _Nullable error) {

            }];
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

调用照相机

-(void)CameraAction
{
    [MyPublicFuntions AlertCameraActions:self FromLocalHandler:^(UIAlertAction * _Nullable Action) {
        [MyPublicFuntions AlertOkOnly:self Message:@"功能未开通,敬请期待!~" OkHandler:nil Completion:nil];
    } FromCameraHandler:^(UIAlertAction * _Nullable Action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController* Picker = [[UIImagePickerController alloc] init];
            Picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            Picker.delegate = self;
            Picker.allowsEditing = YES;
            Picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:Picker animated:YES completion:nil];
        }
    } CancelHandler:nil Completion:nil];

}

获取相册

-(void)GetUpAlbum
{
    NSString* AlbumName = [[NSUserDefaults standardUserDefaults] valueForKey:CNAlbumName];
    PHFetchResult<PHCollection*>* AllAlbums = [PHCollection fetchTopLevelUserCollectionsWithOptions:nil];
    [AllAlbums enumerateObjectsUsingBlock:^(PHCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.localizedTitle isEqualToString:AlbumName] && [obj isMemberOfClass:[PHAssetCollection class]]) {
            _SelectCollection = (PHAssetCollection*) obj;
            _AllAssets = [PHAsset fetchAssetsInAssetCollection:_SelectCollection options:nil];
        }
    }];
}

这个是“图片上传小助手”里面的代码,凑活看吧~

总结:

1,所有对于相册的更新都应该在[phphotolibrary sharedPhotoLibrary ]perform changes里面写,否则错误。

2,更新通知则要实现PHPhotoLibraryChangeObserver协议,在performChange里面的CompletionHandler里面写不好使。

3,系统的相册目前没有找到能修改的方法,报错code=-1.知道的可以留言哈~

参考:http://www.jianshu.com/p/42e5d2f75452

你可能感兴趣的:(PHPhotoLibrary)