iOS开发 PHAsset使用

        // 这时 assetsFetchResults 中包含的,应该就是各个资源(PHAsset)
        for ( PHAsset *phAsset in assets) {
            // 获取一个资源(PHAsset)
            if (phAsset.mediaType == PHAssetMediaTypeVideo) {//视频
                PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
                options.version = PHImageRequestOptionsVersionCurrent;
                options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
                
                PHImageManager *manager = [PHImageManager defaultManager];
                [manager requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
                    AVURLAsset *urlAsset = (AVURLAsset *)asset;
                    NSURL *videoURL = urlAsset.URL;
                    NSURL *mp4 = [self _convert2Mp4:videoURL];
                    NSFileManager *fileman = [NSFileManager defaultManager];
                    if ([fileman fileExistsAtPath:videoURL.path]) {
                        NSError *error = nil;
                        [fileman removeItemAtURL:videoURL error:&error];
                        if (error) {
                            NSLog(@"failed to remove file, error:%@.", error);
                        }
                    }
                    [self sendVideoMessageWithURL:mp4];
                }];
                
            }else{//图片或者gif
                
                PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
                option.resizeMode = PHImageRequestOptionsResizeModeExact;
                option.networkAccessAllowed = YES;
                option.synchronous = YES;//同步执行
                
                [[PHImageManager defaultManager] requestImageDataForAsset:phAsset options:option resultHandler:^(NSData *data, NSString *uti, UIImageOrientation orientation, NSDictionary *dic){
                    if ([uti isEqualToString:@"com.compuserve.gif"]) {
                        if (data != nil) {
                            NSString *path;
                            NSURL *urlPath = [dic objectForKey:@"PHImageFileURLKey"];
                            NSString *fileName = nil;
                            if (urlPath) {
                                fileName = [[urlPath absoluteString] lastPathComponent];
                                path = [self convertGIFData:data fileName:fileName];
                            }
                            [self sendImageMessageWithLocalPath:path ext:@{CHAT_GIF_EXT_KEY: @(YES)}];
                        }
                        return;
                    }
                    
                    NSData *imageDataW = nil;
                    if ([uti isEqualToString:@"public.heif"] || [uti isEqualToString:@"public.heic"]) {
                        CIImage *ciImage = [CIImage imageWithData:data];
                        CIContext *context = [CIContext context];
                        NSData *jpgData = [context JPEGRepresentationOfImage:ciImage colorSpace:ciImage.colorSpace options:@{}];
                        
                        imageDataW = jpgData;
                        
                    } else {
                        imageDataW = data;
                    }
                    
                    //                            if ([imageDataW length] / (1024 * 1024) > 3.0) {
                    //                                imageDataW = [[UIImage imageWithData: imageDataW] compressAndResize]; //图片大于3M压缩操作,可不调用此方法
                    //                            }
                    
                    if (imageDataW != nil) {
                        [self sendImageMessageWithData:imageDataW];
                    } else {
                        [self showHint:NSEaseLocalizedString(@"message.smallerImage", @"The image size is too large, please choose another one")];
                    }
                }];
    
            }
        }

用PHAsset获取照片名称目前网上有三种方法。

第一种:

 [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
    NSURL *url = [info valueForKey:@"PHImageFileURLKey"];
    NSString *str = [url absoluteString];   //url>string
    NSArray *arr = [str componentsSeparatedByString:@"/"];
   NSString *imgName = [arr lastObject];
}]

这种方法是通过获取图片的URL路径,然后再获取图片名称。经过多次测试,图片URL路径有时候可以获取到,有时候获取不到,所以会导致图片名称有时候获取不到,所以这种方法并不靠谱。

第二种

 NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
 NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;

经过测试,这种方法只有在iOS 9.0以上的系统才有作用,iOS8.0用这个方法获取不到,所以这种方法并不靠谱。

第三种(靠谱的方法)

  NSString *filename = [asset valueForKey:@"filename"];

你可能感兴趣的:(iOS开发 PHAsset使用)