lantianyiming 2013-08-08 03:44 采纳率: 0%
浏览 2177
已采纳

下载和设置UIImage不在主线程中

从web服务器中添加一个图片。怎么设置它不在主线程上?我想要视图先加载,然后图片加载。这样用户在体验时不会感到很慢。

就是不知道怎么加到代码中。

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:mainImageUrl]];
UIImage *image = [ [UIImage alloc] initWithData:imageData];

UIView *v = [ [UIView alloc] initWithFrame:CGRectMake(10, 150, 300, 180)];
UIImageView *iv = [ [UIImageView alloc] initWithFrame:CGRectMake(0, 10, 300, 180)];
[iv setImage:image];   
[_scrollView addSubview:v];
[v addSubview:iv];

我想到可以加到线程中的:

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
//
//
dispatch_async( dispatch_get_main_queue(), ^{
    // Add code here to update the UI/send notifications based on the
    // results of the background processing
});
});
  • 写回答

3条回答

  • ReyZhang 移动开发领域新星创作者 2013-08-08 05:39
    关注

    现在有不少处理这种情况的解决方案。有一些第三方的库可以实现这种的异步图片加载,你只需要关注自己的逻辑就可以了。如:EGOImageView ,也可以自己到code4app.com这个网站上去搜一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用