[英]Remotely Loading Images in an iPhone App
我正在制作的應用程序將包含大量圖像。 因此,顯然將它們全部存儲在用戶電話上並占用所有磁盤空間確實很糟糕。 當我剛接觸iOS開發時,如何最好地將圖像遠程加載到UIImageView中。 我有什么選擇?
只是告訴您,如果有幫助,我一次最多只能加載2張圖像。
任何建議將不勝感激。 先感謝您!
您可以使用以下代碼通過解鎖主線程來異步下載映像
NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"Your imageURL"]];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error==nil && [data length]>0)
{
UIImage* img=[UIImage imageWithData:data];
// set the downloaded image to imageview here
}
else
{
NSLog(@"%@",error);
}
});
使用SDImageView或類似的開源,如果要在表中加載圖像,則使用其他選項,請使用Apple的惰性加載
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.