我试图在iOS 8中使用新的照片API获取照片的文件扩展名,但到目前为止还没有找到方法。在iOS 8.0之前,我会使用ALAssetPrepresentation来获得文件扩展名,如:
// Get asset representation from asset
ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];
// Extract file extension from the original file name
NSString* fileExt = [[assetRepresentation filename] pathExtension];
现在有办法获得照片的文件扩展名吗?PS:我正在使用新的照片API,因为我需要访问我的照片应用程序中的所有照片,而ALassetsLibrary只允许访问"最近添加的"照片。
我找到了。
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
NSLog(@"info - %@", info);
}];
PHImageFileDataKey = <PLXPCShMemData: 0x179ab6d0> bufferLength=1384448 dataLength=1384448;
PHImageFileOrientationKey = 1;
PHImageFileSandboxExtensionTokenKey = "c05e608acaf0bb212086ed2d512ccc97ea720ac3;00000000;00000000;0000001a;com.apple.app-sandbox.read;00000001;01000003;0000000000030b8c;/private/var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
PHImageFileURLKey = "file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
PHImageFileUTIKey = "public.jpeg";
PHImageResultDeliveredImageFormatKey = 9999;
PHImageResultIsDegradedKey = 0;
PHImageResultIsInCloudKey = 0;
PHImageResultIsPlaceholderKey = 0;
PHImageResultWantedImageFormatKey = 9999;
这里有一种方法:
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager]
requestImageForAsset:asset
targetSize:CGSizeMake(2048, 2048)
contentMode:PHImageContentModeAspectFit
options:imageRequestOptions
resultHandler:^(UIImage *result, NSDictionary *info) {
}];
文件名包含在信息中。例如:
PHImageFileURLKey = "file:///var/mobile/Media/DCIM/100APPLE/IMG_0066.JPG";
**imageManager.requestImageDataForAsset((images[indexPath.row]作为PHAsset),选项:PHImageRequestOptions(),resultHandler:{imagedata,dataUTI,Orientation,info in
var str:String=(((信息作为字典)["PHImageFileURLKey"])!作为NSURL).lastPathComponent作为字符串)cell.imageName.text=str作为字符串**
这也是从照片中选择原始文件名的一种简单方法
PHAsset *asset; // Init this object with your PHAsset object
//and then use below line
NSLog(@"asset ext : %@",[[asset valueForKey:@"filename"] pathExtension]);