关于alasset读取fullscreen引起的内存问题

asset.defaultRepresentation.fullScreenImage // 旋转过方向的图片

asset.defaultRepresentation.fullResolutionImage // 取原始图片

一般相册图片,就使用上面两种,但是,不建议使用原始图片,因为这个图片很大。

但是如果直接使用第一种,会出现很大的内存问题。释放fullScreenImage后,图片就只能用一次。如果不释放,内存就会增大。

这个问题,也许是能力不足,找了两天。第三天早上,FQ在http://stackoverflow.com/questions/11392268/defaultrepresentation-fullscreenimage-on-alasset-does-not-return-full-screen-ima 这个里面得到了答案。备注一下。后来发现这个找到的方法,内存及图片方向旋转都没有做处理。

在苹果官方 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/ImageIOGuide/imageio_source/ikpg_source.html#//apple_ref/doc/uid/TP40005462-CH218-SW3 找到更好的方法:


/// 第一种方法
+ (UIImage *)imageThumbnailFromAsset:(ALAssetRepresentation *)assetRepresentation maxPixelSize:(NSUInteger)size
{
    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])
    {
        CGImageRef myThumbnailImage = MyCreateThumbnailImageFromData(data, (int)size);
        if (myThumbnailImage) {
            result = [UIImage imageWithCGImage:myThumbnailImage];
            CGImageRelease(myThumbnailImage);
        }
    }
    
    return result;
}

CGImageRef MyCreateThumbnailImageFromData (NSData * data, int imageSize)
{
    CGImageRef        myThumbnailImage = NULL;
    CGImageSourceRef  myImageSource;
    CFDictionaryRef   myOptions = NULL;
    CFStringRef       myKeys[5];
    CFTypeRef         myValues[5];
    CFNumberRef       thumbnailSize;
    
    // Create an image source from NSData; no options.
    myImageSource = CGImageSourceCreateWithData((CFDataRef)data,
                                                NULL);
    // Make sure the image source exists before continuing.
    if (myImageSource == NULL){
        fprintf(stderr, "Image source is NULL.");
        return  NULL;
    }
    
    // Package the integer as a  CFNumber object. Using CFTypes allows you
    // to more easily create the options dictionary later.
    thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &imageSize);
    
    // Set up the thumbnail options.
    myKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
    myValues[0] = (CFTypeRef)kCFBooleanTrue;
    myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
    myValues[1] = (CFTypeRef)kCFBooleanTrue;
    myKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
    myValues[2] = (CFTypeRef)thumbnailSize;
    myKeys[3] = kCGImageSourceShouldCache;
    myValues[3] = (CFTypeRef)kCFBooleanFalse;
    myKeys[4] = kCGImageSourceShouldCacheImmediately;
    myValues[4] = (CFTypeRef)kCFBooleanFalse;
    
    myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
                                   (const void **) myValues, 2,
                                   &kCFTypeDictionaryKeyCallBacks,
                                   & kCFTypeDictionaryValueCallBacks);
    
    // Create the thumbnail image using the specified options.
    myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource,
                                                           0,
                                                           myOptions);
    // Release the options dictionary and the image source
    // when you no longer need them.
    CFRelease(thumbnailSize);
    CFRelease(myOptions);
    CFRelease(myImageSource);
    
    // Make sure the thumbnail image exists before continuing.
    if (myThumbnailImage == NULL){
        fprintf(stderr, "Thumbnail image not created from image source.");
        return NULL;
    }
    
    return myThumbnailImage;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值