2012-05-14 14 views
7

En mi aplicación estoy recuperando un UIImage de la biblioteca de activos, esta imagen tiene metadatos. La aplicación cambia el tamaño y gira la imagen, lo que a su vez crea una nueva imagen. La nueva imagen no tiene los metadatos originales que se esperan, pero ¿cómo vuelvo a agregar los metadatos a la imagen antes de subirlos?UIImage meta data

¡Gracias de antemano!

Respuesta

14

Solucionado por mí mismo, este es un método que utilicé solo en caso de que alguien más se esté preguntando cómo hacerlo. :)

-(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; 
} 
+0

¿Qué es asset_? – bneupaane

+0

Es un ALAsset :) – shoughton123

+0

#import es necesario para obtener las constantes – Anth0