UIImage元数据

时间:2012-05-14 11:54:57

标签: iphone objective-c ios xcode

在我的应用程序中,我从资产库中检索UIImage,此图像包含元数据。然后应用程序调整大小并旋转图像,从而创建新图像。新图像没有预期的原始元数据,但如何在上传之前将元数据添加回图像?

提前致谢!

3 个答案:

答案 0 :(得分:14)

我自己修正了,这是我用过的方法,只是其他人想知道怎么做! :)

-(UIImage *)addMetaData:(UIImage *)image {

    NSData *jpeg = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];

    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL);

    NSDictionary *metadata = [[asset_ defaultRepresentation] metadata];

    NSMutableDictionary *metadataAsMutable = [metadata mutableCopy];

    NSMutableDictionary *EXIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary];
    NSMutableDictionary *GPSDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary];
    NSMutableDictionary *TIFFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
    NSMutableDictionary *RAWDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyRawDictionary];
    NSMutableDictionary *JPEGDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyJFIFDictionary];
    NSMutableDictionary *GIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGIFDictionary];

    if(!EXIFDictionary) {
        EXIFDictionary = [NSMutableDictionary dictionary];
    }

    if(!GPSDictionary) {
        GPSDictionary = [NSMutableDictionary dictionary];
    }

    if (!TIFFDictionary) {
        TIFFDictionary = [NSMutableDictionary dictionary];
    }

    if (!RAWDictionary) {
        RAWDictionary = [NSMutableDictionary dictionary];
    }

    if (!JPEGDictionary) {
        JPEGDictionary = [NSMutableDictionary dictionary];
    }

    if (!GIFDictionary) {
        GIFDictionary = [NSMutableDictionary dictionary];
    }

    [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
    [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];
    [metadataAsMutable setObject:TIFFDictionary forKey:(NSString *)kCGImagePropertyTIFFDictionary];
    [metadataAsMutable setObject:RAWDictionary forKey:(NSString *)kCGImagePropertyRawDictionary];
    [metadataAsMutable setObject:JPEGDictionary forKey:(NSString *)kCGImagePropertyJFIFDictionary];
    [metadataAsMutable setObject:GIFDictionary forKey:(NSString *)kCGImagePropertyGIFDictionary];

    CFStringRef UTI = CGImageSourceGetType(source);

    NSMutableData *dest_data = [NSMutableData data];

    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);

    //CGImageDestinationRef hello;

    CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);

    BOOL success = NO;
    success = CGImageDestinationFinalize(destination);

    if(!success) {
    }

    dataToUpload_ = dest_data;

    CFRelease(destination);
    CFRelease(source);

    return image;
}

答案 1 :(得分:3)

看看这个答案。我认为它会指出你正确的方向。

Email jpg created in app including metadata

答案 2 :(得分:3)

您可以转到Git Hub Project和一些教程链接Adding metadata to iOS images the easy wayGetting metadata from images on iOS。最好的问候;)