xcode 如何更改图像视图中的图像?代码 4.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9266667/
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
How to change the image inside an imageview? xCode 4.2
提问by Nick
I've been looking for hours now and i cant find a solution. i have an imageview, i have declare it as an IBOutlet, inside that imageview i have already put an image using the interface build of xCode 4.2.
我一直在寻找几个小时,但我找不到解决方案。我有一个图像视图,我已将其声明为 IBOutlet,在该图像视图中,我已经使用 xCode 4.2 的界面构建放置了一个图像。
Now my quesion, whats the line of code that it can change the image of the imageview?
现在我的问题是,它可以更改图像视图图像的代码行是什么?
The old ones like UIImage *image = [UIImageView imageNamed:@"image2.png"];
老人们喜欢 UIImage *image = [UIImageView imageNamed:@"image2.png"];
its not working because it says that *image is unused variable. it also gives me error at imageNamed it says No known class method for selector 'imageName'
它不起作用,因为它说 *image 是未使用的变量。它也在 imageNamed 给我错误它说没有已知的选择器'imageName'类方法
Whats the new code in 4.2? or is it me doing something wrong?
4.2 中的新代码是什么?还是我做错了什么?
回答by Caleb
myImageView.image = [UIImage imageNamed:@"image2.png"];
That should do it. UIImageView has an image
property that you can set, either as above, or using the -setImage:
method that the property provides. Also, from the docs:
那应该这样做。UIImageView 有一个image
你可以设置的属性,如上,或者使用-setImage:
该属性提供的方法。此外,来自文档:
Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.
设置图像属性不会改变 UIImageView 的大小。调用 sizeToFit 来调整视图的大小以匹配图像。
Your other questions:
您的其他问题:
its not working because it says that *image is unused variable.
它不起作用,因为它说 *image 是未使用的变量。
In the code you provided, you're assigning the image to a local variable. If you don't then do something with that variable, there's no point in that code. Instead, assign the image to the image
property of your image view, as described above.
在您提供的代码中,您将图像分配给局部变量。如果不这样做,则对该变量执行某些操作,则该代码没有任何意义。相反,将图像分配给image
图像视图的属性,如上所述。
it also gives me error at imageNamed it says No known class method for selector 'imageName'
它也在 imageNamed 给我错误它说没有已知的选择器'imageName'类方法
That just looks like a typo in your code. The lack of a 'd' in 'imageName' is surely the problem.
这看起来像是你的代码中的一个错字。'imageName' 中缺少 'd' 肯定是问题所在。
Whats the new code in 4.2?
4.2 中的新代码是什么?
Nothing has changed with respect to setting the image of an image view, as far as I know. I think you've just got a couple little issues in your code.
据我所知,设置图像视图的图像没有任何变化。我认为您的代码中有几个小问题。
回答by Eimantas
It's you doing something wrong. You are mixing up UIImage
and UIImageView
classes. The former is for representing image data in memory. The latter one is representing UIImage
instances in a view.
是你做错了什么。你是混淆UIImage
和UIImageView
类。前者用于表示内存中的图像数据。后者代表UIImage
视图中的实例。
You should change your line to:
您应该将您的线路更改为:
imageViewOutlet.image = [UIImage imageNamed:@"image2.png"];
Notice that I'm using UIImage
class to assign the image to UIImageView
's image
property.
请注意,我正在使用UIImage
class 将图像分配给UIImageView
的image
属性。
回答by iDifferent
You are trying to declare a new variable and not using the ivar you declared with the IBOutlet
您正在尝试声明一个新变量,而不是使用您通过 IBOutlet 声明的 ivar
回答by Lucio Fonseca
This Works fine for me. oldImage is the IBOutlet of UIImageView. The setCellImage method is inside the Custom UITableViewCell Class.
这对我来说很好用。oldImage 是 UIImageView 的 IBOutlet。setCellImage 方法位于自定义 UITableViewCell 类中。
- (void)setCellImage:(UIImage*)newImage{
[self.oldImage setImage:newImage];
}
回答by zaph
You need to assign
你需要分配
[UIImage imageNamed:@"image2.png"]
to the image
property of the ivar of the IBOutlet
.
到 的image
ivar的属性IBOutlet
。
For example if your IBOutlet is defined:
例如,如果您定义了 IBOutlet:
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
assign like so:
像这样分配:
imageView.image = [UIImage imageNamed:@"image2.png"];