ALAsset 将资源转换为 NSData

原文地址:http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller


ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
         resultBlock:^(ALAsset *asset) {
             ALAssetRepresentation *image_representation = [asset defaultRepresentation];
             // create a buffer to hold image data
             uint8_t *buffer = (Byte*)malloc(image_representation.size);
             NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0  length:image_representation.size error:nil];
             
             if (length != 0)  {
                 
                 // buffer -> NSData object; free buffer afterwards
                 NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];
                 
                 // identify image type (jpeg, png, RAW file, ...) using UTI hint
                 NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];
                 
                 // create CGImageSource with NSData
                 CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) adata,  (__bridge CFDictionaryRef) sourceOptionsDict);
                 
                 // get imagePropertiesDictionary
                 CFDictionaryRef imagePropertiesDictionary;
                 imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);
                 
                 // get exif data
                 CFDictionaryRef exif = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary);
                 NSDictionary *exif_dict = (__bridge NSDictionary*)exif;
                 NSLog(@"exif_dict: %@",exif_dict);
                 
                 // save image WITH meta data
                 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                 NSURL *fileURL = nil;
                 CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, imagePropertiesDictionary);
                 
                 if (![[sourceOptionsDict objectForKey:@"kCGImageSourceTypeIdentifierHint"] isEqualToString:@"public.tiff"])
                 {
                     fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.%@",
                                                       documentsDirectory,
                                                       @"myimage",
                                                       [[[sourceOptionsDict objectForKey:@"kCGImageSourceTypeIdentifierHint"] componentsSeparatedByString:@"."] objectAtIndex:1]
                                                       ]];
                     
                     CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((__bridge CFURLRef)fileURL,
                                                                                 (__bridge CFStringRef)[sourceOptionsDict objectForKey:@"kCGImageSourceTypeIdentifierHint"],
                                                                                 1,
                                                                                 NULL
                                                                                 );
                     CGImageDestinationAddImage(dr, imageRef, imagePropertiesDictionary);
                     CGImageDestinationFinalize(dr);
                     CFRelease(dr);
                 }
                 else
                 {
                     NSLog(@"no valid kCGImageSourceTypeIdentifierHint found …");
                 }
                 
                 // clean up
                 CFRelease(imageRef);
                 CFRelease(imagePropertiesDictionary);
                 CFRelease(sourceRef);
             }
             else {
                 NSLog(@"image_representation buffer length == 0");
             }
         }
        failureBlock:^(NSError *error) {
            NSLog(@"couldn't get asset: %@", error);
        }
 ];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Photos 框架来获取相册中的所有图片,代码示例: ```swift import Photos let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@", "***ONE***") let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions) if let album = collection.firstObject { let assets = PHAsset.fetchAssets(in: album, options: nil) for index in 0..<assets.count { let asset = assets[index] PHImageManager.default().requestImageData(for: asset, options: nil) { (data, _, _, _) in if let imageData = data { // 将 imageData 转换成 CGImage if let image = UIImage(data: imageData)?.cgImage { // 将 CGImage 转换NSData let data = NSMutableData() let dest = CGImageDestinationCreateWithData(data, kUTTypeJPEG, 1, nil)! CGImageDestinationAddImage(dest, image, nil) CGImageDestinationFinalize(dest) // 使用转换后的 NSData 进行处理 } } } } } ``` 这里使用了 `PHAssetCollection.fetchAssetCollections` 方法来获取指定名称为 "***ONE***" 的相册,然后使用 `PHAsset.fetchAssets` 方法来获取相册中的所有照片,最后使用 `PHImageManager.default().requestImageData` 方法来获取每张照片的数据。在获取到数据之后,可以使用 `UIImage(data:)` 方法将数据转换成 UIImage,然后使用 `UIImage.cgImage` 属性将 UIImage 转换成 CGImage。最后可以使用 `CGImageDestinationCreateWithData` 方法将 CGImage 转换NSData。 需要注意的是,这里使用的是 JPEG 格式,如果需要使用其他格式,可以修改 `kUTTypeJPEG` 参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值