我看不到任何图片。为什么?

时间:2011-05-17 14:58:04

标签: iphone uiimageview uiimage

我看不到任何照片。为什么呢?

UIImage *photoImg = [UIImage imageNamed:@"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg"];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];
[self.view addSubview:imageView2];

2 个答案:

答案 0 :(得分:7)

因为如果你使用代码:

[UIImage imageNamed:@"mypicture.png"];

图像应该在iPhone上,而不是从URL加载。从URL您需要使用以下内容:

NSString path = @"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *photoImg = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];

答案 1 :(得分:0)

您尚未设置imageView2

的框架
imageView2.frame = CGRectMake( 0, 0, 100, 100 ); // Or whatever.