Set Maximum Size for CGImageRef from ALAssetRepresentation

自古美人都是妖i 提交于 2019-12-22 11:00:32

问题


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];

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.

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

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.


回答1:


UPDATE:

After some discussion in the comments.

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

[asset 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.

MY OLD ANSWER:

is below, in case any one finds the idea helpful.:

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

Using byte sizes that you would expect a device camera's image would be.

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.)

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.

You can then add the results up and determine if the whole file will be too large.

But never have a buffer/file that is too large



来源:https://stackoverflow.com/questions/8840307/set-maximum-size-for-cgimageref-from-alassetrepresentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!