2011-12-12 17 views
18

Desidero estrarre l'immagine utilizzando ALAssetsLibrary e ALAsset direttamente nella forma di un oggetto NSData.Utilizzo di ALAssetsLibrary e ALAsset estrarre Immagine come NSData

Utilizzo di un NSURL Estrarre l'immagine nel modo seguente.

NSURL *referenceURL =newURL; 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset) 
{ 
    UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]; 
} 

Ora qui prendiamo l'immagine come UIImage, ma ho bisogno di prendere l'immagine direttamente come NSData.

Desidero farlo perché (ho letto che) una volta scattata l'immagine in UIImage, perdiamo tutti i dettagli EXIF ​​dell'immagine.

Questo è il motivo che voglio estrarre l'immagine direttamente come NSData, invece di fare questo

NSData *webUploadData=UIImageJPEGRepresentation(copyOfOriginalImage, 0.5); 

Questo passaggio mi fa perdere tutti i dettagli EXIF.

Si prega di aiuto.

risposta

33
 ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; 
     [assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] resultBlock:^(ALAsset *asset) 
     { 
      ALAssetRepresentation *rep = [asset defaultRepresentation]; 
      Byte *buffer = (Byte*)malloc(rep.size); 
      NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil]; 
      NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want 
      [data writeToFile:photoFile atomically:YES];//you can save image later 
     } 
     failureBlock:^(NSError *err) { 
      NSLog(@"Error: %@",[err localizedDescription]); 
     }]; 
+1

Grazie per la vostra meravigliosa suggestione. Ma, c'è un modo per comprimere l'immagine? Prima di usarlo come NSData. –

+0

Se ho capito bene, l'immagine sarà già compressa. La presentazione predefinita sarà probabilmente un file jpeg o png – HeikoG

+2

Ho un problema con questa tecnica mentre importando più ALAssets allo stesso tim, sembra che il buffer venga riutilizzato per il prossimo elemento. –

-1
UIImage * selImage = [UIImage imageWithCGImage:[asset thumbnail]];  
NSData *baseImage=UIImagePNGRepresentation(selImage); 
+0

sei sicuro ???? –

+0

Questo ha funzionato alla grande per me. – mreynol

0

utilizzando questo codice:

+ (BOOL)exportDataToURL:(NSString *)filePath error:(NSError **)error andAsset:(ALAsset *)asset 
{ 
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath]; 

    if (!handle) 
     return NO; 

    static const NSUInteger BufferSize = 1024 * 1024; 

    ALAssetRepresentation *rep = [asset defaultRepresentation]; 
    uint8_t *buffer = calloc(BufferSize, sizeof(*buffer)); 
    NSUInteger offset = 0, bytesRead = 0; 

    do { 
     @try { 
      bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:error]; 
      [handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]]; 
      offset += bytesRead; 
     } @catch(NSException *exception) { 
      free(buffer); 

      return NO; 
     } 
    } while (bytesRead > 0); 

    free(buffer); 
    return YES; 
}