码迷,mamicode.com
首页 > 其他好文 > 详细

ALAsset 将资源转换为 NSData

时间:2017-07-13 16:07:16      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:public   str   cfs   documents   bytes   meta   rds   clean   lock   

原文地址: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

标签:public   str   cfs   documents   bytes   meta   rds   clean   lock   

原文地址:http://www.cnblogs.com/Walking-Jin/p/7160418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!