2013-06-28 69 views

回答

4

您可以從服務器獲取圖像的具體路徑與圖像名稱的圖像。

UIImage* serverImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://example.com/image_name.png"]]]; 

然後你可以在任何你想要的地方使用serverImage

2

嘗試使用此:

-(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr 
{ 
    NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]]; 
    NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil]; 
    UIImage *image = [UIImage imageWithData:imageData]; 

    return image; 
} 
1

,我用它來裝載圖像,而不會阻塞UI一個非常有用的項目是SDWebImage。 它在UIImageView上添加了一個異步類別,使您只需一行代碼即可加載圖像:

[myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
+0

它阻擋了用戶界面。下載完成後你正在更新你的UI,你應該異步使用它。 SDWebImage有責任在主線程上更新你的用戶界面。 – Roger

+0

沒錯,所以上面的代碼不會阻塞主線程,因爲SDWebImage在後臺線程上下載,然後在主線程上完成後更新。 – Bersaelor