关于图片以及格式UTI
生活随笔
收集整理的這篇文章主要介紹了
关于图片以及格式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 datauint8_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 hintNSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[assetRep UTI] ,kCGImageSourceTypeIdentifierHint,nil];// create CGImageSource with NSDataCGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef)imgData, ( CFDictionaryRef) sourceOptionsDict);// get imagePropertiesDictionary CFDictionaryRef imagePropertiesDictionary;imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);// get exif dataCFDictionaryRef 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 dataNSString *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
總結
以上是生活随笔為你收集整理的关于图片以及格式UTI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OWASP十大漏洞之一
- 下一篇: 2017第九届北京空气净化及水净化、北京