zoukankan      html  css  js  c++  java
  • 关于图片以及格式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
    }

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

  • 相关阅读:
    EChart.js 简单入门(转)
    C# 接口的特点、接口的作用、接口的简单应用1(转)
    C# 接口的特点、接口的作用、接口的简单应用(转)
    jquery实现全选、全消、反选功能
    python函数式编程-------python2.7教程学习【廖雪峰版】(五)
    python高级-------python2.7教程学习【廖雪峰版】(四)
    python函数-------python2.7教程学习【廖雪峰版】(三)
    python基础-------python2.7教程学习【廖雪峰版】(二)
    python语言特性-------python2.7教程学习【廖雪峰版】(一)
    jquery基础研究学习【基础】
  • 原文地址:https://www.cnblogs.com/Peterahan/p/3261617.html
Copyright © 2011-2022 走看看