objective-c – ALAsset上的defaultRepresentation fullScreenImage不返回全屏图像

前端之家收集整理的这篇文章主要介绍了objective-c – ALAsset上的defaultRepresentation fullScreenImage不返回全屏图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我将图像保存为专辑作为资产.我也想检索它们并全屏显示它们.我使用以下代码
ALAsset *lastPicture = [scrollArray objectAtIndex:iAsset]; 

ALAssetRepresentation *defaultRep = [lastPicture defaultRepresentation];

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

问题是返回的图像是零.我在ALAssetRepresentation参考中读到,当图像不适合时,返回nil.

我把这个图像放到了一个大小与iPad屏幕相同的UIImageView上.我想知道你是否可以帮我解决这个问题?

先感谢您.

解决方法

我不是fullScreenImage或fullResolutionImage的粉丝.我发现当你对队列中的多个资产执行此操作时,即使你立即释放UIImage,内存使用量也会大幅增加而不应该增加.此外,当使用fullScreenImage或fullResolutionImage时,返回的UIImage仍然是压缩的,这意味着它将在第一次绘制之前被解压缩,因此在主线程上将阻止您的UI.

我更喜欢使用这种方法.

-(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,(__bridge CFDictionaryRef)options);

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

        if (sourceRef)
            CFRelease(sourceRef);
    }

    return result;
}

你可以像这样使用它:

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

猜你在找的C&C++相关文章