Photo库使用

2017-12-28  本文已影响14人  Vijay_

PHAsset

PHAssetCreationRequest

PHAssetCollection

PHAssetCollectionChangeRequest

PHImageManager

详情类可以通过fetch获取到系统相册里的相关资源,包装的请求类可以对齐进行增删改查创建,图片管理类可以获取到图片对象

所有的修改移动图片操作都要在[[PHPhotoLibrary sharedPhotoLibrary] performChanges:]方法中执行

获取资源方法

   
//    获取到所有的静态资源
    PHFetchResult<PHAsset *> *  assets=[PHAsset fetchAssetsWithOptions:nil];
    for (PHAsset* asset in assets) {
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            NSLog(@"%@",result);
        }];
    }
    
//    获取到所有的相册文件夹
    PHFetchResult<PHAssetCollection *> * collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (PHCollection* collection in collections) {
        NSLog(@"%@",collection.localizedTitle);
    }
    
//    获取到相机胶卷目录
    PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].firstObject;
    NSLog(@"%@",cameraRoll.localizedTitle);
    

保存图片

//保存按钮事件
- (IBAction)savePic:(UIButton *)sender {
    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    switch (status) {
        case PHAuthorizationStatusDenied:
            NSLog(@"授权拒绝");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"系统原因导致无法授权相册");
        case PHAuthorizationStatusNotDetermined:{
            
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                [self saveImgToCustomPic];
            }];
        }
            break;
        case PHAuthorizationStatusAuthorized:{
            [self saveImgToCustomPic];
        }
            break;
            
        default:
            break;
    }
    
    
}



/** 把相机胶卷里的图片保存到自定义相册里*/
- (void)saveImgToCustomPic
{
    __block NSString *assetId = nil;
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    phpLibraryHandler(^{
        //        创建一个静态资源处理对象 (会自动保存图片到相机胶卷)
        assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:_imageView.image].placeholderForCreatedAsset.localIdentifier;
    },^(BOOL success, NSError * _Nullable error) {
        if (!success) {
            [SVProgressHUD showErrorWithStatus:@"保存失败"];
            return ;
        }
        PHAssetCollection *createdCollection = [self getPHAssetCollection];
        phpLibraryHandler(^{
            //                根据相片标识获取到相片对象
            PHAsset *assets = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].lastObject;
            //                获取到已创建相册的请求处理对象
            PHAssetCollectionChangeRequest *collectionRequest =[PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
            //                给相册添加相片资源
            [collectionRequest addAssets:@[assets]];
        }, ^(BOOL success, NSError * _Nullable error) {
            
            if (!success) {
                [SVProgressHUD showErrorWithStatus:@"保存失败"];
                return ;
            }
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        });
        
    });
    
}


/**返回相册对象*/
- (PHAssetCollection*)getPHAssetCollection
{
    PHFetchResult<PHAssetCollection *> * collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (PHAssetCollection* collection in collections) {
        if ([collection.localizedTitle isEqualToString:libraryName]) {
            return collection;
        }
    }
    
    __block NSString *collerctionId = nil;
    //    同步执行相册创建方法
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        collerctionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:libraryName].placeholderForCreatedAssetCollection.localIdentifier;
    } error:nil];
    return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collerctionId] options:nil].lastObject;
    
    
}

获取相册下的所有静态资源

 //    获取到所有的相册文件夹
    PHFetchResult<PHAssetCollection *> * collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (PHAssetCollection* collection in collections) {
        
        //        获取到每个相册
        NSLog(@"%@",collection.localizedTitle);
        
        //        获取到该相册下的所有静态资源
        PHFetchResult<PHAsset *> *  assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
        for (PHAsset* asset in assets) {
            [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                NSLog(@"%@",result);
            }];
        }
        
    }
上一篇下一篇

猜你喜欢

热点阅读