ios - 如何在后台下载图像以在iOS中使用imageView进行设置?

标签 ios objective-c uiimageview

我使用了以下代码,但不适用于我。

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(markerView.bounds.size.width/10, markerView.bounds.size.height/4, markerView.bounds.size.width*4/5, markerView.bounds.size.height/1.5)];

NSString *strPath = [NSString stringWithFormat:@"%@",[[arrTemp valueForKey:@"image"] objectAtIndex:marker.accessibilityLabel.integerValue]];
NSLog(@"img path %@", strPath);

dispatch_async(dispatch_get_main_queue(), ^{
  UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]]];
  imgView.image = img;
});

最佳答案

您可以在下面尝试代码。
它为我工作。

        NSString *str = [NSString stringWithFormat:@"%@",[[[dictTemp valueForKey:@"image"] objectAtIndex:j] valueForKey:@"imagepath"]];
        NSString *strSub = [str stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strSub]];

        dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(q, ^{
            /* Fetch the image from the server... */
            NSData *data = [NSData dataWithContentsOfURL:imgUrl];
            UIImage *img = [[UIImage alloc] initWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^{

                [imageView setImage:img];
            });
        });

关于ios - 如何在后台下载图像以在iOS中使用imageView进行设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35432854/

相关文章:

ios - 如何使用FCM向iOS发送通知?

ios - 如何使 UIImageView 中心的区域透明

ios - didReceiveMemoryWarning 方法中应该释放什么东西?

objective-c - 数学函数未显示正确结果

ios - pushViewController iOS 8 上的黑屏

ios - 如何在设备旋转时缩放 UIImageView?

ios - UIImageView 上的 UIGestureRecognizer

ios - 使用 objective-c 更改所选 UITabBarItem 的字体

ios - 发送通用推送通知 - Xcode

ios - Objective-C - 获取本地化的周末和工作日名称?