简体   繁体   English

iOS-处理大图像

[英]iOS - Dealing with Large Images

I have an iOS app in which I am dealing with variably sized images that are included in links that I download from the web. 我有一个iOS应用程序,用于处理从网络下载的链接中包含的大小可变的图像。 The images don't need to be stored so I am using EGOImageView which is a UIImageView extension that caches images in the background and these images are in a UITableView. 图像不需要存储,因此我正在使用EGOImageView,它是UIImageView扩展,可在后台缓存图像,而这些图像在UITableView中。 All of this works perfectly for images of below 1000x1000 or so pixels but the bigger pictures are a problem. 所有这些都非常适合像素小于1000x1000的图像,但是较大的图片是个问题。 They download in the background just fine but just setting the cached image as the UIImageViews image takes so much time for the really big images that it makes scrolling through the Table View very choppy when there are many large images. 他们在后台下载的效果很好,但只是设置缓存的图像,因为UIImageViews图像需要花费大量时间来处理真正的大图像,以至于当有许多大图像时,在“表视图”中滚动变得非常不稳定。

My question then is what can I do to make it so that simply loading these images into a UIImageView is not a huge burden on the main thread? 那么我的问题是我该怎么做,以便将这些图像简单地加载到UIImageView中不会对主线程造成太大的负担?

I had to do some work around this recently, 我最近不得不为此做一些工作,

My solution was quite simple. 我的解决方案很简单。

  1. Download the image (AFNetworking) and store it in the documents directory 下载图像(AFNetworking)并将其存储在documents目录中
  2. Resize the image (Core Graphics) to create a "thumbnail" version for when you are scrolling 调整图像大小(核心图形)以在滚动时创建“缩略图”版本
  3. when the user presses the cell to see the image - show the larger image. 当用户按下单元格以查看图像时-显示较大的图像。

You can use AFNetworking to download the image to the disk and then resize using Core Graphics. 您可以使用AFNetworking将图像下载到磁盘,然后使用Core Graphics调整大小。 I wrapped mine up in an NSOperation and added them to a queue so it didn't freeze up any UI. 我将我的程序包装在一个NSOperation中,并将它们添加到队列中,这样它就不会冻结任何UI。

The reason you are seeing a laggy response is that those images are getting held in memory which is fine for smaller tables but it isn't scaleable. 您看到反应迟钝的原因是这些图像被保存在内存中,这对于较小的表是可以的,但它不可伸缩。 It could also be because your fetch is being done on the main thread. 也可能是因为您的提取是在主线程上完成的。

Hope this has sent you in the right direction. 希望这能给您正确的方向。

https://github.com/AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking

CoreGraphics Image resize CoreGraphics图像调整大小

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

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