简体   繁体   English

如何通过代码更改UIImageView图像内容?

[英]How to change the UIImageView image content by code?

I use this to assign image: 我用它来分配图像:

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"]];
CGRect cropRect = CGRectMake(175, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
imageView.image = [UIImage imageWithCGImage:imageRef];
[self addSubview:imageView];
CGImageRelease(imageRef);

Now, I want to change the image from myImage to myImage2, so I do that: 现在,我想将图像从myImage更改为myImage2,所以我这样做:

    UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage2" ofType:@"png"]];
CGRect cropRect = CGRectMake(175, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
imageView.image = [UIImage imageWithCGImage:imageRef];
[self addSubview:imageView];

It works, but not the way I want, I want to change the image, not add a imageView on top of the pervious one. 它工作,但不是我想要的方式,我想改变图像,而不是在上一个上面添加一个imageView。 How can I modify my code? 我该如何修改我的代码? thx u. 谢谢你。

Alternatively, this can be done with tags. 或者,这可以使用标签来完成。

//Set up the imageView in viewDidLoad or similar
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
[imageView setTag:123];
[self.view addSubview:imageView];
[imageView release];

Then any time you want to change the image, you just have to use: 那么每当你想要改变图像时,你只需要使用:

[(UIImageView*)[self.view viewWithTag:123] setImage:[UIImage imageWithCGImage:imageRef]];

Just another option, the other answers will work just as well. 只是另一种选择,其他答案也会起作用。

I just spent a day looking at this tricky issue, so I thought I would share how to change images on a UIImageView. 我只花了一天时间看这个棘手的问题,所以我想我会分享如何在UIImageView上更改图像。 Too many posts regarding this were not able to show a simple way of doing this so here goes... 关于这个的太多帖子都无法显示这样做的简单方法所以这里...

If you want to change an image displayed on an imageView 如果要更改imageView上显示的图像

In your .h 在你的.h

IBOutlet UIImageView *imageView;
@property (nonatomic, retain, readwrite) IBOutlet UIImageView *imgView;

and in your .m 在你的.m

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(133, 230, 60, 60)];
    self.imageView.image = [UIImage imageNamed:@"firstPicure.png"];
    [self.view addSubview:self.imageView];
}

Now change your image displayed in your imageView. 现在更改imageView中显示的图像。 One nice thing about this method is that imageNamed will return an autorelease instance of an image so you can avoid memory leaks and for simplicity sake you use performSelectorOnMainThread to handle the change 关于这个方法的一个好处是imageNamed将返回一个图像的自动释放实例,这样你就可以避免内存泄漏,为简单起见,你使用performSelectorOnMainThread来处理这个变化

-(void)changeImage
{
    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:@"secondPicture.png"] waitUntilDone:YES];
}

I am not sure why you are bothering to crop the image; 我不确定你为什么要打扰裁剪图像; just set the contentMode property appropriately. 只需适当设置contentMode属性。 Alternative approach: 替代方法:

UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImage *image2 = [UIImage imageNamed:@"myImage2.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:...];
imageView.contentMode = UIViewContentModeTopLeft;
imageView.image = image;
[self.view addSubView:imageView];
...
imageView.image = image2;

At the moment, you're creating a new UIImageView and adding it to the list of sub views. 目前,您正在创建一个新的UIImageView并将其添加到子视图列表中。 As you're seeing, you then end up with two image views. 正如您所看到的那样,您最终得到了两个图像视图。 What you want is to just change the image displayed by the first view. 你想要的只是改变第一个视图显示的图像。 Retain a handle to the UIImageView you create (probably as a member field), and when you want to change the image, just write 保留您创建的UIImageView的句柄(可能作为成员字段),当您想要更改图像时,只需写入

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myImage2" ofType:@"png"]];
imageView.image = [UIImage imageWithCGImage:imageRef];

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

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