关于图片以及格式UTI

Uniform Type Identifiers 

定义:http://www.escape.gr/manuals/qdrop/UTI.html 

使用UTI获取图片格式:

ALAssetRepresentation *assetRep = [asset defaultRepresentation];
NSString *mediaType = [assetRep UTI];

最近做了一个模块,需要获取到ios 图片的详细属性:包括创建时间,文件格式等等。由于AALsset中,包含的内容不详细,只好自己去获取。查询到需要使用ImageIO接口

首先要导入 AssetsLibrary.framework and ImageIO.framework.

代码部分:

#import <ImageIO/ImageIO.h>
- (NSString *)infoFromImageIO:(ALAssetRepresentation *)assetRep
{
    NSMutableData *imgData = nil;
    NSString *mediaType = [assetRep UTI];
    // create a buffer to hold image data
    uint8_t *buffer = (Byte*)malloc(assetRep.size);
    NSUInteger length = [assetRep getBytes:buffer fromOffset:0.0  length:assetRep.size error:nil];
    
    if (length > 0)
    {
        imgData = [[NSMutableData alloc] initWithBytesNoCopy:buffer length:length freeWhenDone:YES];
    }
    
    // identify image type (jpeg, png, RAW file, ...) using UTI hint
    NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[assetRep UTI] ,kCGImageSourceTypeIdentifierHint,nil];
    
    // create CGImageSource with NSData
    CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef)imgData,  ( CFDictionaryRef) sourceOptionsDict);
    
    // get imagePropertiesDictionary
    CFDictionaryRef imagePropertiesDictionary;
    imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);
    
    // get exif data
    CFDictionaryRef exif = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyExifDictionary);
    NSDictionary *exif_dict = (NSDictionary*)exif;
    
    if (exif_dict && [exif_dict count] > 0)
    {
        //获取创建时间
        NSString *createTime = [NSString stringWithString:[exif_dict objectForKey:@"DateTimeOriginal"]];
        CFRelease(imagePropertiesDictionary);
        CFRelease(sourceRef);
        return createTime;
    }
    
    return nil;
#if 0
    // 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);
#endif
}

因为我这个接口只需要获取时间,故屏蔽了一部分代码,使用屏蔽部分的内容可以将相册中的图片保存到我们自己应用的沙盒中。

原文地址:https://www.cnblogs.com/Peterahan/p/3261617.html