gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:47:52 24 4
gpt4 key购买 nike

在我的应用程序中,我将图像作为 Assets 保存到相册中。我还想检索它们并全屏显示它们。我使用以下代码:

ALAsset *lastPicture = [scrollArray objectAtIndex:iAsset]; 

ALAssetRepresentation *defaultRep = [lastPicture defaultRepresentation];

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

问题是返回的图像是nil。我在 ALAssetRepresentation 引用资料中读到,当图像不适合时,它会返回 nil。

我将此图像放入具有 iPad 屏幕大小的 UIImageView。我想知道你是否可以帮我解决这个问题?

提前致谢。

最佳答案

我不喜欢 fullScreenImage 或 fullResolutionImage。我发现,当您对队列中的多个 Assets 执行此操作时,即使您立即释放 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, 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;
}

你可以这样使用它:

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

关于objective-c - ALAsset 上的 defaultRepresentation fullScreenImage 不返回全屏图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11392268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com