如何使用像http://img.abc.com/noPhoto4530.gif这样的链接创建带有图像的UIImageView?
答案 0 :(得分:43)
NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
答案 1 :(得分:2)
如果你想在后台下载图片,然后在主线程上设置它,你可以这样做:
- (void)downloadPicture {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
UIImage *image = [self getPicture:url];
dispatch_async(dispatch_get_main_queue(), ^{
[self setPicture:image];
});
});
}
- (UIImage *)getPicture:(NSURL *)pictureURL {
NSData *data = [NSData dataWithContentsOfURL:pictureURL];
UIImage *image = [UIImage imageWithData:data];
return image;
}
- (void)setPicture:(UIImage *)image {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(kPictureX, kPictureY, image.size.height, image.size.width)];
[imageView setImage:image];
[self.view addSubview: imageView];
}
答案 2 :(得分:2)
以下是那些希望使用iOS 7的新NSURLSession类套件的代码片段:
// Set NSURLSessionConfig to be a default session
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
// Create session using config
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// Create URL
NSURL *url = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"];
// Create URL request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";
// Create data task
NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Okay, now we have the image data (on a background thread)
UIImage *image = [UIImage imageWithData:data];
// We want to update our UI so we switch to the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Create image view using fetched image (or update an existing one)
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Do whatever other UI updates are needed here on the main thread...
});
}];
// Execute request
[getDataTask resume];
答案 3 :(得分:1)
将图片下载到设备上的本地路径,然后从imageWithContentsOfFile
获取UIImage,并使用此设置UIImageView
中的图片。记得有时清理你的图像文件。
答案 4 :(得分:1)
下载图像后,您还需要将其作为子视图放置在视图中,如下所示:
NSURL *url = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[someOtherView addSubview:myImageView];
[myImageView release];
答案 5 :(得分:0)
您可以按照here所述进行操作,也可以使用NSURLConnection下载图像数据并创建UIImage以设置为UIImageView。我个人更喜欢使用NSURLConnection异步下载图像。
答案 6 :(得分:0)
这里可以有相同的答案
NSURL *urlLink = [NSURL URLWithString:@"http://img.abc.com/noPhoto4530.gif"];
NSData *dataURL = [NSData dataWithContentsOfURL:urlLink];
UIImage *imageData = [UIImage imageWithData:dataURL];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imageData];
答案 7 :(得分:0)
你可以试试现代gcd风格(xcode 8.0 +):
let queue = DispatchQueue(label: "com.mydomain.queue3")
queue.async {
let imageURL: URL = URL(string: "https://www.brightedge.com/blog/wp-content/uploads/2014/08/Google-Secure-Search_SEL.jpg")!
guard let imageData = try? Data(contentsOf: imageURL) else {
return
}
DispatchQueue.main.async {
self.imageView.image = UIImage(data: imageData)
}
}
您还可以将第一个DispatchQueue
替换为URLSession.dataTask
let imageURL: URL = URL(string: "https://www.brightedge.com/blog/wp-content/uploads/2014/08/Google-Secure-Search_SEL.jpg")!
(URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: imageURL, completionHandler: { (imageData, response, error) in
if let data = imageData {
print("Did download image data")
DispatchQueue.main.async {
self.imageView.image = UIImage(data: data)
}
}
}).resume()