簡體   English   中英

如何從iPhone中的URL獲取圖像

[英]how to get image from url in iphone

我是iPhone新手,我想從url獲取圖像

我嘗試了休閑代碼

-(void)viewDidLoad
{
 UIImageView *UV=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
 [self.view addSubview:UV];    
 NSURL *url = [NSURL URLWithString:@"http://www.google.com/recaptcha/api/noscript?k=6LdON9USAAAAAO9AtfFsuLxBY1l-yNDBoJcb1Jeb"];

    NSData *myData = [NSData dataWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc] initWithData:myData];
    UV.image=image;
}

但是圖片未在圖片視圖中顯示。任何人能對我有幫助嗎?謝謝

您提供的網址不是圖片,因此無法使用。

更重要的是,盡管看起來還不錯,但絕對不建議這樣做。

dataWithContentsOfURL:的調用在下載期間阻止了主線程,從而使應用程序無響應。

看一下Antaresm提到的一些開源項目。
您可能還需要查看AFNetworking ,將其放入您的項目中,然后,您所要做的就是

[UV setImageWithURL:url];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM