如何用像這樣的鏈接的圖像創建UIImageView http://img.abc.com/noPhoto4530.gif?如何使用鏈接中的圖像創建UIImageView?
20
A
回答
42
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
將圖像下載到設備上的本地路徑,然後從imageWithContentsOfFile
獲取UIImage,然後使用它在UIImageView
中設置圖像。記得在某個時候清理你的圖像文件。
0
您可以按照描述here執行操作,也可以使用NSURLConnection下載圖像數據並創建UIImage以設置爲您的UIImageView。我個人更喜歡使用NSURLConnection來異步下載鏡像。
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];
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
這裏是爲那些希望使用的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];
+0
無論如何,接受的答案是相當古老和不好的做法。這是正確的答案(當然有適當的錯誤處理;))。 – 2015-03-04 01:47:33
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];
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)
}
}
你也可以用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()
更換一次
DispatchQueue
相關問題
- 1. 用鏈接創建UIImageView圖像?
- 2. 如何在UIImageView中創建大圖像?
- 3. Dojo使用鏈接創建圖像
- 4. 使用link_to創建圖像鏈接
- 5. 如何使用jQuery在圖像上創建鏈接
- 6. 如何使用php和css創建圖像翻轉鏈接
- 7. 如何使用html DOM重新創建圖像src鏈接?
- 8. 如何使用圖像創建BMVC(TwitterBootstrapMVC)鏈接?
- 9. 如何使JavaScript創建圖像的鏈接
- 10. 從圖像中創建的HTML鏈接使用Ruby
- 11. 如何爲滑塊中的每個圖像創建鏈接
- 12. 如何在CGI腳本中的圖像上創建鏈接?
- 13. 如何創建鏈接到Rails 3中的文本和圖像?
- 14. 如何使用javascript添加鏈接到使用javascript創建的圖像元素
- 15. UIImageView UIViewContentModeScaleToFill - 使用此鏈接時鏈接
- 16. 如何在Android中創建圖像和鏈接按鈕(java)?
- 17. 如何在smarty中創建圖像下載鏈接
- 18. 如何在此jQuery Slider插件中爲圖像創建鏈接
- 19. 如何在textview中創建鏈接以打開圖像
- 20. 如何使用uiimageview查看圖像中的圖像
- 21. 如何使用XML視圖在SAPUI5中創建鏈接?
- 22. 如何使用C#創建鏈接?
- 23. 如何使用python包創建鏈接
- 24. 如何使用XSLT創建超鏈接?
- 25. 如何在程序中創建使用Interface Builder創建的UIImageView中的圖像高度/寬度?
- 26. 如何在構建UIImageView時創建圖像
- 27. 從Backstretch圖像創建鏈接?
- 28. 如何僅使用代碼創建UIImageView?
- 29. 如何使用Resources.resx鏈接圖像
- 30. 如何創建包含圖像超鏈接的JTable單元格?
感謝您的幫助:) – 2010-08-25 08:55:19
完美的作品!無法想象「dataWithContentsOfURL」處於其靜態方法中。順便說一句,我正在使用Xamarin。 – Howard 2014-03-07 02:11:27
我可以超時使用imageview嗎?當互聯網連接速度慢時,應用程序可能會崩潰,所以我想添加超時時間@nszombie – 2014-04-04 09:29:49