简体   繁体   English

在iOS中处理大图像

[英]Handle large images in iOS

I want to allow the user to select a photo, without limiting the size, and then edit it. 我想让用户在不限制大小的情况下选择照片,然后进行编辑。

My idea is to create a thumbnail of the large photo with the same size as the screen for editing, and then, when the editing is finished, use the large photo to make the same edit that was performed on the thumbnail. 我的想法是创建一个与用于编辑的屏幕尺寸相同的大照片的缩略图,然后,在编辑完成后,使用大照片进行与缩略图相同的编辑。

When I use UIGraphicsBeginImageContext to create a thumbnail image, it will cause a memory issue. 当我使用UIGraphicsBeginImageContext创建缩略图时,它将导致内存问题。

I know it's hard to edit the whole large image directly due to hardware limits, so I want to know if there is a way I can downsample the large image to less then 2048*2048 wihout memory issues? 我知道由于硬件限制很难直接编辑整个大图像,所以我想知道是否有一种方法可以将大图像下采样到不到2048 * 2048而没有内存问题?

I found that there is a BitmapFactory Class which has an inSampleSize option which can downsample a photo in Android platform. 我发现有一个BitmapFactory类,它有一个inSampleSize选项,可以对Android平台中的照片进行下采样。 How can this be done on iOS? 怎么能在iOS上完成?

You need to handle the image loading using UIImage which doesn't actually load the image into memory and then create a bitmap context at the size of the resulting image that you want (so this will be the amount of memory used). 您需要使用UIImage来处理图像加载, UIImage实际上并不将图像加载到内存中,然后以您想要的结果图像的大小创建位图上下文(因此这将是使用的内存量)。 Then you need to iterate a number of times drawing tiles from the original image (this is where parts of the image data are loaded into memory) using CGImageCreateWithImageInRect into the destination context using CGContextDrawImage . 然后需要迭代的次数绘制从原始图像块(这是图像数据的部分被加载到存储器),使用CGImageCreateWithImageInRect到使用目的地上下文CGContextDrawImage

See this sample code from Apple . 请参阅Apple的此示例代码

Large images don't fit in memory. 大图像不适合内存。 So loading them into memory to then resize them doesn't work. 因此将它们加载到内存中然后调整它们的大小不起作用。

To work with very large images you have to tile them. 要处理非常大的图像,您必须将它们平铺。 Lots of solutions out there already for example see if this can solve your problem: 已经有很多解决方案,例如,看看这是否可以解决您的问题:

https://github.com/dhoerl/PhotoScrollerNetwork https://github.com/dhoerl/PhotoScrollerNetwork

I implemented my own custom solution but that was specific to our environment where we had an image tiler running server side already & I could just request specific tiles of large images (madea server, it's really cool) 我实现了我自己的自定义解决方案,但这是特定于我们的环境,我们有一个运行服务器端的图像管理器已经和我可以请求特定的大图像区块(制作服务器,它真的很酷)

The reason tiling works is that basically you only ever keep the visible pixels in memory, and there isn't that many of those. 平铺工作的原因是,基本上你只能将可见像素保留在内存中,并且没有那么多。 All tiles not currently visible are factored out to the disk cache, or flash memory cache as it were. 当前不可见的所有切片都会被分解为磁盘高速缓存或闪存高速缓存。

Take a look at this work by Trevor Harmon. 看看Trevor Harmon的这项工作。 It improved my app's performance.I believe it will work for you too. 它改善了我的应用程序的性能。我相信它也适合你。

https://github.com/coryalder/UIImage_Resize https://github.com/coryalder/UIImage_Resize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM