2012-07-09 8 views
6

En mi aplicación, guardo imágenes en un álbum como activo. También quiero recuperarlos y mostrarlos en pantalla completa. Yo uso el siguiente código:defaultRepresentation fullScreenImage en ALAsset no devuelve la imagen de pantalla completa

ALAsset *lastPicture = [scrollArray objectAtIndex:iAsset]; 

ALAssetRepresentation *defaultRep = [lastPicture defaultRepresentation]; 

UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
              scale:[defaultRep scale] orientation: 
    (UIImageOrientation)[defaultRep orientation]]; 

El problema es que la imagen es devuelta nula. He leído en la referencia ALAssetRepresentation que cuando la imagen no se ajusta, se devuelve nula.

Pongo esta imagen en un UIImageView que tiene el tamaño de la pantalla del iPad. Me preguntaba si podrías ayudarme con este problema.

Gracias de antemano.

+0

De este código todo se ve muy bien y trabajando también en mi aplicación de este código proporcionar toda la imágenes bien. Puedo decir que los activos recuperados serán nulos o que hay algún problema con ese código, no este –

Respuesta

8

No soy fanático de fullScreenImage o fullResolutionImage. Descubrí que cuando haces esto en múltiples activos en una cola, incluso si liberas el UIImage inmediatamente, el uso de la memoria aumentará dramáticamente mientras que no debería. Además, al utilizar FullScreenImage o fullResolutionImage, el UIImage devuelto todavía está comprimido, lo que significa que se descomprimirá antes de dibujarse por primera vez, por lo tanto, en el hilo principal que bloqueará su UI.

Prefiero usar este método.

-(UIImage *)fullSizeImageForAssetRepresentation:(ALAssetRepresentation *)assetRepresentation 
{ 
    UIImage *result = nil; 
    NSData *data = nil; 

    uint8_t *buffer = (uint8_t *)malloc(sizeof(uint8_t)*[assetRepresentation size]); 
    if (buffer != NULL) { 
     NSError *error = nil; 
     NSUInteger bytesRead = [assetRepresentation getBytes:buffer fromOffset:0 length:[assetRepresentation size] error:&error]; 
     data = [NSData dataWithBytes:buffer length:bytesRead]; 

     free(buffer); 
    } 

    if ([data length]) 
    { 
     CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil); 

     NSMutableDictionary *options = [NSMutableDictionary dictionary]; 

     [options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceShouldAllowFloat]; 
     [options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceCreateThumbnailFromImageAlways]; 
     [options setObject:(id)[NSNumber numberWithFloat:640.0f] forKey:(id)kCGImageSourceThumbnailMaxPixelSize]; 
     //[options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceCreateThumbnailWithTransform]; 

     CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (__bridge CFDictionaryRef)options); 

     if (imageRef) { 
      result = [UIImage imageWithCGImage:imageRef scale:[assetRepresentation scale] orientation:(UIImageOrientation)[assetRepresentation orientation]]; 
      CGImageRelease(imageRef); 
     } 

     if (sourceRef) 
      CFRelease(sourceRef); 
    } 

    return result; 
} 

Se puede utilizar la siguiente manera:

// Get the full image in a background thread 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 

    UIImage* image = [self fullSizeImageForAssetRepresentation:asset.defaultRepresentation]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 

    // Do something with the UIImage 
    }); 
}); 
Cuestiones relacionadas