从ALAssetRepresentation设置CGImageRef的最大大小 [英] Set Maximum Size for CGImageRef from ALAssetRepresentation

查看:211
本文介绍了从ALAssetRepresentation设置CGImageRef的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下代码从iOS应用程序的ALAsset中获取UIImage。

I am currently using the following code to get an UIImage from an ALAsset in an iOS app.

ALAssetRepresentation *rep = [self.asset defaultRepresentation];
CGImageRef ref = [rep fullResolutionImage];
ALAssetOrientation orientation = [[self.asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
UIImage *image = [UIImage imageWithCGImage:ref scale:1.0 orientation:(UIImageOrientation)orientation];

只要图像相当小(例如使用设备的相机拍摄),这个效果非常好。但是,如果图像太大(6000px ^ 2和更多),应用程序将在此步骤内耗尽:

This works very well as long as the image is reasonably small (e.g. taken with the device's camera). However if the image is too large (6000px^2 and more), the app will run out of memory during this step:

CGImageRef ref = [rep fullResolutionImage];

我想在应用崩溃之前检测这种情况,或者(理想情况下)设置

I would like to detect this condition before the app crashes or (ideally) set a maximum size for the returned image.

我知道 fullScreenImage 会返回一个较小的图片,但是应用需要更高的分辨率

I am aware that fullScreenImage will return a smaller image, however the app requires a much higher resolution from the image for zooming.

此外,使用资源的字节大小是不可靠的,因为高度压缩的格式(例如JPG)在打包大小上可能很小,但仍然消耗

Furthermore, using the byte size of the asset is unreliable as highly compressed formats such as JPG may well be small in packed size but still consume too much memory if their size in pixels is high (low detail images, highly compressed images).

任何有关如何获取图像尺寸并检测这种情况的输入内容(如果图像大小以像素为单位)因此,我可以取消加载并显示错误或如何获得较小版本的这种大型资产。

Any input on how to either get the dimensions of the image and detect this situation beforehand so I can cancel loading and display an error or how to get a smaller version of such a large asset would be greatly appreciated.

推荐答案

p> UPDATE:

UPDATE:

在评论中进行了一些讨论。

After some discussion in the comments.

您可以获取文件/资产的Exif信息与调用

You can obtain the files/asset's Exif info with the call

[asset metadata]

我发现的参考是 sarofox .alasset-image-metadata
该页面还显示如何获取单个属性。
这应该允许您在打开文件/资产之前获取像素大小。

The reference I found is here sarofox.alasset-image-metadata The page also shows how to get individual properties. This should allow you to get the pixel sizes before you open the file/asset.

我的老答案:

如下:

这只是一个猜测。
但是不能使用 ALAssetRepresentation getBytes:fromOffset:length:error:

This is only a guess. But could you not use ALAssetRepresentation getBytes:fromOffset:length:error:

使用fromOffset从文件文件大小开始执行检查。

Run a check with a fromOffset from the start of the file size of the file. And a length to the ( wanted maximum ) end of the file size.

读取缓冲区中的大小。如果它小于请求,那么你是确定。
如果它与请求的范围完全相同,那么文件中可能会有更多剩余(可能更大。)

Read the size in the buffer. If it is smaller than the request then you are ok. If it is exactly the same as the range requested then there may be more left in the file (It maybe bigger.)

如果是,请运行新请求 getBytes:fromOffset:length:error: *此次使用fromOffset从最后一个请求的长度和新的长度。

If so, Run a new request of getBytes:fromOffset:length:error:* this time with a fromOffset from the length of the last request and new length.

但是从来没有太大的缓冲区/文件

But never have a buffer/file that is too large

这篇关于从ALAssetRepresentation设置CGImageRef的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆