2010-08-25 46 views

回答

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]; 
+0

感謝您的幫助:) – 2010-08-25 08:55:19

+0

完美的作品!無法想象「dataWithContentsOfURL」處於其靜態方法中。順便說一句,我正在使用Xamarin。 – Howard 2014-03-07 02:11:27

+0

我可以超時使用imageview嗎?當互聯網連接速度慢時,應用程序可能會崩潰,所以我想添加超時時間@nszombie – 2014-04-04 09:29:49

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
相關問題