• 关于图片以及格式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
    }

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

  • 相关阅读:
    了解mysql之约束篇
    快速了解MySQL中的字符串数据类型
    解决tomcat命令行输出的中文乱码问题
    浅谈了解JDBC
    .NET Core项目部署到Linux(Centos7)(八)为.NET Core项目创建Supervisor进程守护监控
    .NET Core项目部署到Linux(Centos7)(七)启动和停止.NET Core项目
    .NET Core项目部署到Linux(Centos7)(六)发布.NET Core 项目到Linux
    .NET Core项目部署到Linux(Centos7)(五)Centos 7安装.NET Core环境
    .NET Core项目部署到Linux(Centos7)(四)VMware Workstation虚拟机Centos 7安装
    .NET Core项目部署到Linux(Centos7)(三)创建.NET Core API项目
  • 原文地址:https://www.cnblogs.com/Peterahan/p/3261617.html
Copyright © 2020-2023  润新知