2017-06-06 2 views
1

J'essaie de copier une image ui et de modifier les métadonnées sans réencoder l'image afin de ne pas provoquer d'artefacts jpg (je connais le code ci-dessous reencodes, mais c'est juste pour qu'il puisse être exécuté essai). CGImageDestinationCopyImageSource est supposé copier la source d'une image sans la réencoder, mais lorsque j'utilise cette fonction, elle échoue à CGImageDestinationFinalize. J'essaie de suivre cette note technique https://developer.apple.com/library/content/qa/qa1895/_index.html Une idée pour laquelle CGImageDestinationFinalize échouerait avec le code ci-dessous?CGImageDestinationFinalize échoue lors de l'utilisation CGImageDestinationCopyImageSource

-(NSData *)copyImageAndChangeMetaData:(UIImage *)image { 

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 

    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL); 
    NSLog(@" source: 0x%x", source); 
    if (source == NULL) { 
     NSLog(@" source is NULL"); 
    } 

    CFStringRef UTI = CGImageSourceGetType(source); 
    NSLog(@" UTI: %@", (__bridge NSString *)UTI); 
    if (UTI == NULL) { 
     NSLog(@" UTI is NULL"); 
    } 

    NSMutableData *dest_data = [NSMutableData data]; 
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL); 
    if (!destination) 
    { 
     NSLog(@"Image Data Error: Could not create image destination"); 
    } 

    CFMutableDictionaryRef metadata = CFDictionaryCreateMutable(kCFAllocatorDefault, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

    int orient = 8; 
    CFNumberRef orientRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &orient); 
    CFDictionarySetValue(metadata, kCGImageDestinationOrientation, orientRef); 

    CFStringRef dateStringRef = CFStringCreateWithCString(kCFAllocatorDefault, "2017-06-06T17:31:46Z", kCFStringEncodingASCII); 
    CFDictionarySetValue(metadata, kCGImageDestinationDateTime, dateStringRef); 

    CFErrorRef errorRef = nil; 
    if (!CGImageDestinationCopyImageSource(destination, source, metadata, &errorRef)) { 
     CFStringRef error_description = CFErrorCopyDescription(errorRef); 

     NSString *errorDescription = (__bridge NSString *)error_description; 
     NSLog(@"Image Data Error: Error copying image data: %@", errorDescription); 

     CFRelease(error_description); 
    } 

    BOOL success = NO; 
    success = CGImageDestinationFinalize(destination); 
    if (!success) 
    { 
     NSLog(@"Image Data Error: Could not finalize image"); 
    } 
    if (source != NULL) { 
     CFRelease(source); 
    } 

    if (destination != NULL) { 
     CFRelease(destination); 
    } 

    return dest_data; 
} 

Répondre

3

Selon la documentation de HeaderDoc d'Apple pour CGImageDestinationCopyImageSource() fonction dans le fichier <ImageIO/CGImageDestination.h>:

CGImageDestinationFinalize() ne doit pas être appelé par la suite - le résultat est enregistré à la destination lorsque ce de retour de la fonction.

+0

C'est une API très étrange, toutes choses considérées. –