为目标 c 中的指针设置值?
我有 2 个 UIImageVIew:
UIImageView *imageView1 = [[UIImageView alloc] init];
UIImageView *imageView2 = [[UIImageView alloc] init];
和一个 UIImage:
UIImage *image = [[UIImage alloc] imageWithNamed:@"image_name.png"];
所以我使用相同的 UIImage 为两个 UIImageView 设置图像:
imageView1.image = image;
imageView2.image = image;
如何更改 UIImage *image 的值(不将 UIImage *image 指向另一个 UIImage:<强>图像= [[UIImage分配] imageWithNamed:@“another_image_name.png”]; )。所以当我这样做时,我希望 imageView1 和 imageView2 显示相同的图像(another_image_name.png)
I have 2 UIImageVIew:
UIImageView *imageView1 = [[UIImageView alloc] init];
UIImageView *imageView2 = [[UIImageView alloc] init];
and one UIImage:
UIImage *image = [[UIImage alloc] imageWithNamed:@"image_name.png"];
so i set image for both of UIImageView with the same UIImage:
imageView1.image = image;
imageView2.image = image;
How can i change value of UIImage *image (Not point UIImage *image to another UIImage by: image = [[UIImage alloc] imageWithNamed:@"another_image_name.png"];). So when i do it, i want imageView1 and imageView2 display the same image (another_image_name.png)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIImage
实例是不可变的,一旦它们被alloc
'd和init
'd,它们就不能改变。如果您想更改图像视图的图像,则需要alloc
和init
另一个 UIImage 实例(或使用便捷方法,例如+imageNamed:< /code> 等)并将其设置为您的两个图像视图。
UIImage
instances are immutable, once they have beenalloc
'd andinit
'd, they cannot change. If you want to change the images for your image views, you need toalloc
andinit
another UIImage instance (or use the convenience methods, e.g.+imageNamed:
etc) and set it to both of your image views.您无法修改 UIImage,因此您所要求的内容不可能直接完成。
但是,您可以使用 键值观察,当观察到变化时,它将其图像设置为与 imageView1 中相同的图像。
在 imageView2 中,您将需要以下内容。您可以子类化 UIImageView 或向其添加类别。
You can't modifty a UIImage so what you ask is impossible to do straightforwardly.
However, you could have imageView2 register as an observer on the image property of imageView1y using key value observing and when it observes a change it sets its image to the same one as in imageView1.
In imageView2, you'll need the following. You can either subclass UIImageView or add a category to it.