为目标 c 中的指针设置值?

发布于 2024-09-30 10:30:43 字数 638 浏览 3 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-10-07 10:30:43

UIImage instances are immutable, once they have been alloc'd and init'd, they cannot change. If you want to change the images for your image views, you need to alloc and init another UIImage instance (or use the convenience methods, e.g. +imageNamed: etc) and set it to both of your image views.

Image objects are immutable, so you cannot change their properties after creation

梦在夏天 2024-10-07 10:30:43

您无法修改 UIImage,因此您所要求的内容不可能直接完成。

但是,您可以使用 键值观察,当观察到变化时,它将其图像设置为与 imageView1 中相同的图像。

// To register for KVO
[imageView1 addObserver: imageView2
             forKeyPath: @"image"
                options: NSKeyValueObservingOptionNew
                context: NULL];

在 imageView2 中,您将需要以下内容。您可以子类化 UIImageView 或向其添加类别。

// method in imageView2 to deal with observations

- (void)observeValueForKeyPath: (NSString*) keyPath
                      ofObject: (id)object
                        change: (NSDictionary*)change
                       context: (void*) context
{
    if ([keyPath isEqual:@"image"]) 
    {
        [self setImage: [change objectForKey:NSKeyValueChangeNewKey]];
    }
}

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.

// To register for KVO
[imageView1 addObserver: imageView2
             forKeyPath: @"image"
                options: NSKeyValueObservingOptionNew
                context: NULL];

In imageView2, you'll need the following. You can either subclass UIImageView or add a category to it.

// method in imageView2 to deal with observations

- (void)observeValueForKeyPath: (NSString*) keyPath
                      ofObject: (id)object
                        change: (NSDictionary*)change
                       context: (void*) context
{
    if ([keyPath isEqual:@"image"]) 
    {
        [self setImage: [change objectForKey:NSKeyValueChangeNewKey]];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文