我使用了以下代码,但不适用于我。
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/