Why I can't update my UIImageView?

Posted by Tattat on Stack Overflow See other posts from Stack Overflow or by Tattat
Published on 2010-05-03T16:14:06Z Indexed on 2010/05/03 16:18 UTC
Read the original article Hit count: 400

I make my own UIImageView, like this:

@interface MyImageView : UIImageView{


}

And I have the initWithFrame like this:

- (void)initWithFrame{

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

I want to change the image when the user touchesBegin, so I have something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

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

I find that I can't change the image. So, I add a line [self addSubview:imageView]; in the touchesBegan method, that's work. But I have no idea on why I can't change the image from the touchesBegan, thz.

© Stack Overflow or respective owner

Related posts about uiimageview

Related posts about iphone