[英]how can I display UIImage from image url
如果我靜態給出以下代碼,則可以正常工作,但
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://4cing.com/mobile_app/uploads/pageicon/6.jpg"]]]];
相同的代碼-我正在動態傳遞圖片網址名稱,因此無法正常工作
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]]]];
NSMutableArray *imagename = [[NSMutableArray alloc] initWithObjects:@"http://4cing.com/mobile_app/uploads/pageicon/6.jpg", nil];
UIImageView *imgDescView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 200, 200)];
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]]]]];
它正在工作...我認為您沒有正確創建數組。
您需要驗證NSArray
內容。 注銷數組的對象:
NSLog(@"imagename = %@",[imagename description]);
另外,附帶說明-可能需要考慮動態加載該圖像。 我寫了一堂課,讓這件事變得很輕松:
https://stackoverflow.com/a/9786513/585320
答案是針對在TableViews
使用(滯后會真正影響性能),但是您可以(而且我也可以)將其用於任何Web圖像加載。 這樣可以使UI保持流暢並且非常快。
嗨,以這種方式創建網址
[NSURL URLWithString:[[NSString stringWithFormat:@"%@",[imagename objectAtIndex:0]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
我認為它將起作用,並將解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.