ALAsset 将资源转换为 NSData

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
 ];

 

以上是关于ALAsset 将资源转换为 NSData的主要内容,如果未能解决你的问题,请参考以下文章

使用 exif 将原始图像从 ALAsset 对象写入文档目录文件夹

将 NSData 转换为 NSString 并再次转换为 NSData?

将 GIF 图像转换为 nsdata

将字符串转换为 NSData 并将 NSData 转换回字符串

将 NSData 转换为 NSDictionary

将 UIImage 转换为 NSData 并在 Swift 中转换回 UIImage?