ios - 使用 grand central dispatch 从文件加载 UIImage

标签 ios grand-central-dispatch

我正在尝试使用 gcd 在后台加载图像。我的第一次尝试没有成功:

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

注释掉后台队列代码并只留下主队列调度 block 是行不通的:

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

在 gcd block 中做 ui 东西有什么技巧吗?

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

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

  });

});

我对其进行了简化,以便在主队列中运行所有内容,但即便如此,它仍然无法正常工作。我想如果我不能让整个事情在主队列中工作,后台队列的东西就不会工作。


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

我在一个全新的项目中尝试了上述技术(gcd 和所有),它确实按预期工作。只是不在我当前的项目中。所以我将不得不做一些缓慢而痛苦的消除工作来找出问题所在。我正在使用此背景加载在 uiscrollview 中显示图像。事实证明,有时会出现一些图像,但并非总是如此。我会弄个水落石出....

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

看起来问题与 UIScrollView 有关,所有这些都在里面。我认为东西没有在应该绘制/刷新的时候绘制/刷新

最佳答案

我运行了你的代码。它有一个异常(exception)。您的意思是 self.view 而不是 self 吗?

案例一:

路径在属性中声明。
imageView 在 ivar 中声明

- (IBAction)buttonImagePressed:(id)sender 
{
    self.path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"picture1.jpg"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{
        UIImage* img = [[UIImage alloc] initWithContentsOfFile: self.path];  
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView = [[[UIImageView alloc] initWithImage:  img] autorelease];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            CGRect photoFrame = self.view.frame;
            imageView.frame = photoFrame;
            [self.view addSubview:imageView];
        });
    });
}

案例二:

path 和 imageView 都是 ivars。 - 没有使用任何属性。

- (IBAction)buttonImagePressed:(id)sender 
{
// will crash if path is defined here
//    path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"picture1.jpg"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{
        //if path is defined here then it will work
        path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"picture1.jpg"];
        UIImage* img = [[UIImage alloc] initWithContentsOfFile:path];
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView = [[[UIImageView alloc] initWithImage:  img] autorelease];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            CGRect photoFrame = self.view.frame;
            imageView.frame = photoFrame;
            [self.view addSubview:imageView];
        });
    });
}

在案例 2 中,它崩溃了,控制台中的消息说要在 apple.com 上提交错误 ...

关于ios - 使用 grand central dispatch 从文件加载 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10692864/

相关文章:

ios - 在 DispatchAfter 中将 DispatchTime 作为参数传递

ios - 无法从没有窗口的 View 中显示弹出窗口 - presentPopoverFromBarButtonItem

ios - UILabel 的文本在延迟后加载

iphone - 在 iTunes 连接中删除新版本的应用程序

ios - 滚动tableview时编辑导航栏的alpha

ios - GCD 调度队列是否足以将核心数据上下文限制在单个线程中

objective-c - 在 macOS 应用程序中,我有一个后台线程执行一行代码,需要花费大量时间。如何取消封禁?

ios - 在 Swift 中使用 dispatch_async 并发分析一个数组

ios - 可以同时申请 iOS Developer Program($99/年)和 iOS Developer Enterprise Program($299/年)吗?

ios - 为什么我无法获得 MKDirectionResponse