2012-07-09 7 views
6

Uygulamamda görüntüleri bir albüm olarak öğeler olarak kaydediyorum. Onları da almak ve tam ekranda görüntülemek istiyorum. Ben şu kodu kullanın:defaultDeğerleme tamScreenImage on ALAsset tam ekran görüntüsünü döndürmez

ALAsset *lastPicture = [scrollArray objectAtIndex:iAsset]; 

ALAssetRepresentation *defaultRep = [lastPicture defaultRepresentation]; 

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

sorun döndü görüntü nil olmasıdır. ALAssetRepresentation referansında, görüntü uygun olmadığında nil olarak döndüğümde okudum.

Bu resmi iPad ekranının boyutuna sahip bir UIImageView ürününe koydum. Bu konuda bana yardım edip edemeyeceğini merak ediyordum?

Önceden teşekkür ederiz.

+0

tüm ince ve bu kod benim uygulamada da tüm görüntüleri cezası sağlamak çalışma görünüyor bu kodu seçin. Alınan varlıkların sıfır olacağını ya da bu kodla ilgili bir sorun olmadığını söyleyebilirim. –

cevap

8

Ben fullScreenImage veya fullResolutionImage hayranı değilim. Bunu bir sıradaki birden çok öğe üzerinde yaptığınızda, UIImage'ı hemen serbest bıraksanız bile, bellek kullanımının önemli ölçüde artacağını ve bunu yapmaması gerektiğini fark ettim. Ayrıca fullScreenImage veya fullResolutionImage kullanıldığında, döndürülen UIImage hala sıkıştırılır, yani ilk kez çekilmeden önce sıkıştırılmış olacak, böylece UI'nizi engelleyecek ana iş parçacığı olacaktır.

Bu yöntemi kullanmayı tercih ediyorum.

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

Böyle kullanabilirsiniz:

// 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 
    }); 
});