Photokit的增加,删除,查询

最近项目中在做一个社交圈的功能,可以选取相册中的图片分享一些图片,在iOS8.0之后,用主要用photokit来对照片进行选取,iOS官方也写了一个官方的demophotokitAppleDemo,是用collectionview来做边查询边展示的,我主要介绍一下对相册的增删查。

  • 第一查询最近添加相册
    //  2 206 最近添加
//获得PHAssetCollectionTypeSmartAlbum  PHAssetCollectionSubtypeSmartAlbumRecentlyAdded类型的所有相册,系统中的最近添加相册只有一个,可以直接取第一个元素。
    PHFetchResult *assetSmartCollection=[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumRecentlyAdded options:nil];
    if (assetSmartCollection4.count>0) {
      PHAssetCollection*recentlyAddedAlbum= assetSmartCollection.firstObject
      NSString*albumName= recentlyAddedAlbum.localizedTitle;
    //按照相片的创造时间查询相册里面的图片
     PHFetchOptions *photosOptions = [[PHFetchOptions alloc] init];
        photosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    //每一个asset对应一个图片
     PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:collection options:photosOptions];
      //相册中图片的数量
      NSInterger photoTotalCount= assets.count;
    }
    
  • 根据PHAsset获取具体的图片
        // 同步获得图片, 只会返回1张图片
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
      //是否同步,设为YES只会返回你设置图片大小尺寸的图片,设为NO之后,会返回一张缩略图和一张你设置尺寸的图片
        options.synchronous = YES;
        options.resizeMode=PHImageRequestOptionsResizeModeFast;
        options.networkAccessAllowed=YES;
        // 是否要原图
        CGFloat scale = [UIScreen mainScreen].scale;
        CGSize cellSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/3.0, [UIScreen mainScreen].bounds.size.width/3.0);
        CGSize size = CGSizeMake(cellSize.width * scale, cellSize.height/2.0 * scale);
        // 从asset中获得图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            if (result) {
                _imageObject=result;  
            }
        }];
  • 增加一个名字为HelloWorld的相册
        __block PHObjectPlaceholder *albumPlaceholder;
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"HelloWorld"];
            albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                    NSLog(@"添加相册成功");
                } completionHandler:^(BOOL success, NSError *error) {
                    if (!success) {
                        NSLog(@"Error creating asset: %@", error);
                    }
                }];
  • 向HelloWorld相册添加一张照片
  PHAssetCollection*oneAlbumColleciton;
//获取所有的相册信息 (在查询相册信息中已讲),然后遍历,根据名字找到HelloWorld相册
 if ([oneAlbumColleciton.localizedTitle isEqualToString:@"HelloWorld"]) {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:saveImage];
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection: oneAlbumColleciton];
        [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            DLOG(@"Error creating asset: %@", error);
        }else{
           
        }
    }];
        }
  • 在相册中删除某张照片
 PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:photosOptions];
 [assetResult enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    if ([asset isEqual:obj]) {
                        //删除图片
                        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                            [PHAssetChangeRequest deleteAssets:@[obj]];
                        } completionHandler:^(BOOL success, NSError *error) {
                            DLOG(@"currentThread:%d",[NSThread isMainThread]);
                            if (success) {     
                             else{
                                    NSLog(@"Error: %@", error);    
                            }
                        }];
               }
            }];

你可能感兴趣的:(Photokit的增加,删除,查询)