繁体   English   中英

“没有可见的@interface声明选择器”错误

[英]“No visible @interface declares the selector” error

我在视图控制器中插入了一个图像视图,但实际上并不知道如何在其上显示图像。

我试过这个:

- (void)viewDidLoad
{
[super viewDidLoad];


id path = @"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *firstImage = [[UIImage alloc] initWithData:data cache:NO];
}

但后来它说'UIimage的无可见@interface声明了选择器initwithData:cache'

我尝试将此行添加到.h文件中但不起作用:

@property (nonatomic, retain) UIImageView *firstImage;

我知道最后一步是将.h create class分配给viewcontroller中的UIimage。

您可以尝试使用此代码从imageview的路径添加图像

 NSURL *url = [NSURL URLWithString:@"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg"];
 NSData *data = [NSData dataWithContentsOfURL:url];
 UIImage *image = [UIImage imageWithData:data];
 imageView = [[UIImageView alloc] initWithImage:image];

不要忘记,以显示您imageView作为出口xib如果通过GUI添加,否则将其添加到您的视图作为subview

尝试[[UIImage alloc] initWithData:data]; 您可以使用NSCachedURLResponse缓存响应。

您可以使用它来从URL加载图像:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

但这会阻止您的应用,直到您的图片加载。 在使用Web上的图像时,最好以异步方式加载图像。 如果图像是在不同的线程中下载的,则用户可以继续与应用程序进行交互。 您可以从Apple查看有关延迟加载图像的示例: LazyTableImages

或者您可以查看本教程以获取异步图像下载: 带有下载图像的UItables

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM