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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15260307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:55:06  来源:igfitidea点击:

"No visible @interface declares the selector" error

iosxcodeuiimageviewxcode4.5

提问by Norm

I inserted an image view in my view controller but dont actually know how to display image on it.

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

I tried this:

我试过这个:

- (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];
}

But then it says that 'No visible @interface for UIimage declares the selector initwithData:cache'

但是然后它说'没有可见的@interface for UIimage 声明了选择器 initwithData:cache'

I tried adding this line to the .h file but does not work either:

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

@property (nonatomic, retain) UIImageView *firstImage;

I know that the last step would be to assign that .h create class to the UIimage in the viewcontroller.

我知道最后一步是将 .h 创建类分配给视图控制器中的 UIimage。

回答by nsgulliver

You can use try this code to add image from the path in your imageview

您可以尝试使用此代码从路径中添加图像 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];

Don't forget to show your imageViewas outlet to xibif you are adding through GUI otherwise add it to your view as subview

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

回答by danh

Try [[UIImage alloc] initWithData:data];you can cache using an NSCachedURLResponsefor the response.

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

回答by AndrejKolar

You can use this to load images from URLs:

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

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

But this will block your app until your image loads. It's best to load your images asynchronously when using images from the web. If the images are downloaded in a different thread user can keep interacting with the app. You can check this example from apple about lazy loading images: LazyTableImages

但这会阻止您的应用程序,直到您的图像加载为止。使用来自 Web 的图像时,最好异步加载图像。如果图像在不同的线程中下载,用户可以继续与应用程序交互。你可以从苹果查看这个关于延迟加载图像的例子:LazyTableImages

Or you can checkout this tutorial for async image download: UItables with downloaded images

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