Iphone 为什么我能';更新我的UIImageView?

Iphone 为什么我能';更新我的UIImageView?,iphone,objective-c,uiimageview,Iphone,Objective C,Uiimageview,我创建自己的UIImageView,如下所示: @interface MyImageView : UIImageView{ } - (id)initWithFrame{ if( ( self = [ super initWithFrame: CGRectMake(0, 0, 175, 175) ] ) ) { UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForRes

我创建自己的UIImageView,如下所示:

@interface MyImageView : UIImageView{


}
- (id)initWithFrame{
    if( ( self = [ super initWithFrame: CGRectMake(0, 0, 175, 175) ] ) ) {

    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 setImage:[UIImage imageWithCGImage:imageRef]];


    CGImageRelease(imageRef);
    }
    return self;

}
- (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);
}
我的框架是这样的:

@interface MyImageView : UIImageView{


}
- (id)initWithFrame{
    if( ( self = [ super initWithFrame: CGRectMake(0, 0, 175, 175) ] ) ) {

    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 setImage:[UIImage imageWithCGImage:imageRef]];


    CGImageRelease(imageRef);
    }
    return self;

}
- (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);
}
我想在用户触摸Begin时更改图像,因此我有如下内容:

@interface MyImageView : UIImageView{


}
- (id)initWithFrame{
    if( ( self = [ super initWithFrame: CGRectMake(0, 0, 175, 175) ] ) ) {

    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 setImage:[UIImage imageWithCGImage:imageRef]];


    CGImageRelease(imageRef);
    }
    return self;

}
- (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);
}

我发现我无法改变图像。因此,我添加了一行[self addSubview:imageView];在touchesbeated方法中,这就是工作。但是我不知道为什么我不能从触摸开始更改图像,太赫兹。

为什么你要从
initWithFrame
返回
void

您应该从
initWithFrame
返回初始化的对象

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