95 votes

Interpréter les métadonnées XMP dans ALAssetRepresentation

Lorsqu'un utilisateur apporte des modifications (recadrage, la suppression des yeux rouges, ...) pour des photos dans le haut- Photos.app sur iOS, les modifications ne sont pas appliquées à l' fullResolutionImage retournée par la correspondante ALAssetRepresentation.

Toutefois, les modifications sont appliquées à l' thumbnail et de la fullScreenImage retournée par l' ALAssetRepresentation. En outre, des informations sur les modifications effectuées peuvent être trouvés dans l' ALAssetRepresentations'dictionnaire de métadonnées via la clé @"AdjustmentXMP".

Je voudrais appliquer ces modifications à l' fullResolutionImage moi-même afin de préserver la cohérence. J'ai trouvé que sur iOS6+ CIFilters' filterArrayFromSerializedXMP: inputImageExtent:error: pouvez convertir cette XMP-métadonnées à un tableau d' CIFilter':

ALAssetRepresentation *rep; 
NSString *xmpString = rep.metadata[@"AdjustmentXMP"];
NSData *xmpData = [xmpString dataUsingEncoding:NSUTF8StringEncoding];

CIImage *image = [CIImage imageWithCGImage:rep.fullResolutionImage];

NSError *error = nil;
NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData 
                                             inputImageExtent:image.extent 
                                                        error:&error];
if (error) {
     NSLog(@"Error during CIFilter creation: %@", [error localizedDescription]);
}

CIContext *context = [CIContext contextWithOptions:nil];

for (CIFilter *filter in filterArray) {
     [filter setValue:image forKey:kCIInputImageKey];
     image = [filter outputImage];
}

Toutefois, cela ne fonctionne que pour certains filtres (recadrage, amélioration automatique) mais pas pour d'autres, comme la suppression des yeux rouges. Dans ces cas, l' CIFilters n'ont aucun effet visible. Donc, mes questions:

  • Quelqu'un est-il au courant d'une façon de créer de la suppression des yeux rouges CIFilter? (D'une manière cohérente avec les Photos.app. Le filtre avec la touche kCIImageAutoAdjustRedEye n'est pas suffisant. E. g., il ne prend pas de paramètres pour la position des yeux.)
  • Est-il possible de générer et d'appliquer ces filtres sous iOS 5?

2voto

Yash Golwara Points 561
ALAssetRepresentation* representation = [[self assetAtIndex:index] defaultRepresentation];

// Create a buffer to hold the data for the asset's image
uint8_t *buffer = (Byte*)malloc(representation.size); // Copy the data from the asset into the buffer
NSUInteger length = [representation getBytes:buffer fromOffset: 0.0  length:representation.size error:nil];

if (length==0)
    return nil;

// Convert the buffer into a NSData object, and free the buffer after.

NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:representation.size freeWhenDone:YES];

// Set up a dictionary with a UTI hint. The UTI hint identifies the type
// of image we are dealing with (that is, a jpeg, png, or a possible
// RAW file).

// Specify the source hint.

NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:

(id)[representation UTI], kCGImageSourceTypeIdentifierHint, nil];

// Create a CGImageSource with the NSData. A image source can
// contain x number of thumbnails and full images.

CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef) adata,  (CFDictionaryRef) sourceOptionsDict);

[adata release];

CFDictionaryRef imagePropertiesDictionary;

// Get a copy of the image properties from the CGImageSourceRef.

imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);

CFNumberRef imageWidth = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelWidth);

CFNumberRef imageHeight = (CFNumberRef)CFDictionaryGetValue(imagePropertiesDictionary, kCGImagePropertyPixelHeight);

int w = 0;

int h = 0;

CFNumberGetValue(imageWidth, kCFNumberIntType, &w);

CFNumberGetValue(imageHeight, kCFNumberIntType, &h);

// Clean up memory

CFRelease(imagePropertiesDictionary);

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X