我用這個來分配圖像:如何通過代碼更改UIImageView圖像內容?
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);
現在,我想改變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];
它的工作原理,但沒有辦法,我想,我想改變圖像,而不是在前面的圖像上添加imageView。我如何修改我的代碼?謝謝你。
@Adam_Wright什麼是imageRef簡單起見?你有沒有留下一條線,或者我應該知道這是什麼?謝謝。 – StoneBreaker 2012-02-29 23:20:59
@StoneBreaker它在提問者代碼中提到。 – 2012-03-01 12:56:39