【问题标题】:Get image Asynchronously and setting setNeedsDisplay异步获取图像并设置 setNeedsDisplay
【发布时间】:2013-02-04 02:18:59
【问题描述】:

此代码位于设置自定义单元格元素的单元格初始化例程中。它从网络异步获取图像。但是一旦完成,我需要它重新绘制。

这是我的sn-p代码:

dispatch_async(myCustomQueue, ^{

    //Look for the image in a repository, if it's not there
    //load the image from the web (a slow process) and return it
    mCover.image = [helperMethods imageManagerRequest:URL];

    //Set the image to be redrawn in the next draw cycle
    dispatch_async(dispatch_get_main_queue(), ^{
        [mCover setNeedsDisplay];
    });

});

但它不会重绘 UIImageView。我也尝试重新绘制整个单元格,但这也不起作用。非常感谢您的帮助。我一直在努力解决这个问题!

【问题讨论】:

  • 您返回的是 UIImage 还是 UIImageView?也许您无意中将图像分配给图像视图?

标签: objective-c setneedsdisplay dispatch-async


【解决方案1】:

而不是setNeedsDisplay,您应该像Apple 在their documentation 中提到的那样在主线程上设置图像。

注意:在大多数情况下,UIKit 类只能从 应用程序的主线程。对于类来说尤其如此 派生自 UIResponder 或涉及操纵您的 应用程序的用户界面。

这应该可以解决您的问题:

dispatch_async(myCustomQueue, ^{

    //Look for the image in a repository, if it's not there
    //load the image from the web (a slow process) and return it
    UIImage *image = [helperMethods imageManagerRequest:URL];

    //Set the image to be redrawn in the next draw cycle
    dispatch_async(dispatch_get_main_queue(), ^{
        mCover.image = image;
    });

});

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多