【发布时间】:2011-09-13 19:41:45
【问题描述】:
我有一组图片网址。我想将图像显示到 UIImageView 中。
现在我将 URL 转换为 NSData,然后将其转换为 UIImage,然后尝试将其加载到 UIImageView。
但是这需要很多时间。
有没有更好的方法可以更快更好地加载图像?
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4 uiimageview
我有一组图片网址。我想将图像显示到 UIImageView 中。
现在我将 URL 转换为 NSData,然后将其转换为 UIImage,然后尝试将其加载到 UIImageView。
但是这需要很多时间。
有没有更好的方法可以更快更好地加载图像?
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4 uiimageview
[UIImage imageWithData:[NSData dataWithContentsOfURL:]]
【讨论】:
在一个语句中使用所有内容。
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];
【讨论】:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
【讨论】:
试试这个:-
image 是 UIImage,imageView 是 UIImageView
NSData *receivedData = [NSData dataWithContentsOfURL:@"yoururl"];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
[self.imageView setImage:self.image];
【讨论】:
尽管此处的所有答案都告诉您在一行代码中执行此操作,但遗憾的是,这对 URL 连接速度或数据/图像解码没有影响。如果您想要一种更快的方式来键入代码,那很好,但我会使用添加到 UIImageView 的类别......
@interface UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl;
@end
@implementation UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl {
NSURL *url = [NSURL urlWithString:aUrl];
NSData *data = [NSData dataWithContentsOfURL:url]
UIImage *image = [UIImage imageWithData:data];
if(image != nil) {
[self setImage:image];
}
}
@end
现在您可以包含标题并执行...
[myImageView loadFromUrl:@"http://myurl.com/image.jpg"];
更多类别(我会将这个添加到我的列表中!)查看here. 这些都是我有用的,你可能会发现它们也很有用! :)
【讨论】:
myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myurl.com/image.jpg"]]];和[myImageView loadFromUrl:@"http://myurl.com/image.jpg"];的问题