使用中央集中调度从文件加载UIImage [英] Load UIImage from file using grand central dispatch

查看:63
本文介绍了使用中央集中调度从文件加载UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gcd在后台加载图像.我的第一次尝试无效:

I'm trying to load images in the background using gcd. My first attempt didn't work:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{
  dispatch_async(dispatch_get_main_queue(), ^{
    // create UI Image, add to subview
  });
});

注释掉后台队列代码并仅保留主队列调度块不起作用:

commenting out the background queue code and leaving just the main queue dispatch block didn't work:

dispatch_async(dispatch_get_main_queue(), ^{
  // create UI Image, add to subview
});

在gcd块中执行ui东西有技巧吗?

Is there some trick to doing ui stuff inside a gcd block?

作为对mattjgalloway的评论的回应,我只是试图在后台线程中加载大图像.这是我最初尝试的完整代码:

In response to mattjgalloway's comment, I'm simply trying to load a big image in a background thread. Here's the full code that I tried originally:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{

    UIImage* img = [[UIImage alloc] initWithContentsOfFile: path];

    dispatch_async(dispatch_get_main_queue(), ^{

       imageView = [[[UIImageView alloc] initWithImage:  img] autorelease];
       imageView.contentMode = UIViewContentModeScaleAspectFill;
       CGRect photoFrame = self.frame;
       imageView.frame = photoFrame;
       [self addSubview:imageView];

  });

});

我简化了它,以便我可以在主队列中运行所有内容,但是即使那样也没有用.我认为如果不能让整个事情都在主队列中工作,那么后台队列中的东西将无法工作.

I simplified it so that I was running everything inside the main queue, but even then it didn't work. I figure if I can't get the whole thing to work in the main queue, no way the background queue stuff will work.

================更新=============

================ UPDATE ==============

我在一个全新的项目中尝试了上述技术(gcd和所有技术),并且确实按预期工作了.只是不在我当前的项目中.因此,我将不得不执行一些缓慢而痛苦的消除工作过程,以找出问题所在.我正在使用此后台加载在uiscrollview中显示图像.事实证明有时会出现一些图像,但并非总是如此.我将深入探讨这个问题....

I tried the above technique (gcd and all) in a brand new project and it does indeed work as expected. Just not in my current project. So I'll have to do some slow, painful process of elimination work to figure out what's going wrong. I'm using this background loading to display images in a uiscrollview. Turns out bits of the images do sometimes show up, but not always. I'll get to the bottom of this....

================更新=============

================ UPDATE ==============

问题似乎与UIScrollView有关,所有这些都在内部.我认为东西应该在什么时候被吸引/刷新

Looks like the issue is related to the UIScrollView all of this is inside. I think stuff isn't getting drawn/refreshed when it should

推荐答案

尝试在[self addSubview:imageView]之后立即调用setNeedDisplay,因为大多数UIKit都不是线程安全的,我不特别了解UIImage方法(在iOS4及更高版本上可能还可以),但我不会这样做.如果要从后台加载图像,最好使用线程安全的ImageIO或Core Graphics函数.
一种替代方法是在后台线程上将图像数据加载到NSData对象,然后在主线程上调用[UIImage alloc]inithWithData: data].

Try to call setNeedDisplay right after [self addSubview:imageView], by the way most of UIKit isn't thread safe, I don't know about UIImage methods specifically (maybe is ok on iOS4 and higher), but I wouldn't do that. If you want to load images from background better use ImageIO that is thread safe, or Core Graphics functions.
An alternative could be load an NSData object with your image data on a background thread an later call [UIImage alloc]inithWithData: data] on the main thread.

这篇关于使用中央集中调度从文件加载UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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