2012-06-15 131 views
0

我有下面的代碼,我主要從這裏得到,但我的圖像仍然沒有被裁剪,我做錯了什麼?我的圖像沒有被剪裁

@synthesize TopImage; 
@synthesize BottomImage; 
@synthesize RotaterImage; 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    TopImage = [UIImage imageNamed:@"zero.png"]; 
    [TopImage drawAtPoint:CGPointMake(0, 0)]; 
    [self cropImage:TopImage:0]; 
    [self addSubview:[[UIImageView alloc] initWithImage:TopImage]]; 
//  BottomImage = [UIImage imageNamed:@"zero.png"]; 
//  [BottomImage drawAtPoint:CGPointMake(0, 55)]; 
//  [self cropImage:BottomImage:50]; 
//  [self addSubview:[[[UIImageView alloc] initWithImage:BottomImage]retain]]; 
//  RotaterImage = [UIImage imageNamed:@"zero.png"]; 
//  [RotaterImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:0]; 
//  [self addSubview:[[[UIImageView alloc] initWithImage:RotaterImage]retain]]; 
//  [self cropImage:RotaterImage:50]; 
    } 
    return self; 
} 

-(void)cropImage:(UIImage *)image:(int)startCroppingPosition{ 
    CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40); 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect); 
    // or use the UIImage wherever you like 
    image = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
} 

我是很新的IOS所以任何幫助表示讚賞

+0

http://stackoverflow.com/a/10868876/1378447 –

回答

2

你正在創建一個新的裁剪圖像,但沒有說init方法吧:)

試試這個變化:

-(UIImage *)cropImage:(UIImage *)image:(int)startCroppingPosition{ 
    CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40); 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect); 
    // or use the UIImage wherever you like 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return newImage; 
} 

,並在init,更換

[TopImage drawAtPoint:CGPointMake(0, 0)]; 

UIImageView* topImageView = [[UIImageView alloc] initWithImage:topImage]; 
topImageView.frame = CGRectMake(x,y,width,height); 

您的裁剪方法現在返回新剪裁的圖像並將其存儲在TopImage中。


其他建議:

類名稱以CapitalLetter,變數開始小寫。 TopImage的確應該是topImage

而且,我總是使用命名參數在你的方法,所以

-(void)cropImage:(UIImage *)image:(int)startCroppingPosition 

應該像

-(void)cropImage:(UIImage *)image startPosition:(int)startCroppingPosition 

那將使你的代碼更可讀性更強,當你回來吧在一個月的時間裏(我已經瞭解到過去的困難方式!)

+0

drawAtPoint不會返回任何內容,但從cropImage函數返回的圖像確實顯示裁剪後的圖像。我不明白的是爲什麼我不能將圖像設置爲新的裁剪的圖像= [UIImage imageWithCGImage:imageRef]; ?? – L7ColWinters

+0

你只是在你的方法中設置'圖像'指針來指向你的新圖像。您根本不會影響'init'方法中的'TopImage'指針,它仍然會指向舊的'UIImage'。如果您想要在cropImage方法中影響'TopImage'指針,您需要將指針傳遞給指針或直接在'cropImage'中設置'TopImage'。然而,指向指針的指針非常棘手,並且設置'TopImage'不會讓你使用'cropImage'來裁剪其他東西。我會返回一個新圖像,並處理更新'init'方法中'TopImage'指向的位置 – deanWombourne

+0

呵呵,我認爲UIImage *是一個指針,並且將新圖像設置爲它將意味着使用等號運算符。所以我不得不做UIImage **,然後做*圖像? – L7ColWinters

0

你只改變圖像和的UIImageView的不是圖像,

我會做的是取代:

[self addSubview:[[UIImageView alloc] initWithImage:TopImage]]; 

UIImageView *iv = [[UIImageView alloc] initWithImage:TopImage]]; 
iv.tag = 1000 //number does not matter 
[self.view addSubview:iv]; 
[iv release]; 

然後裁剪圖像時,只需添加:

UIImageView *iv = [self.vief viewWithTag:1000]; 
iv.image = [UIImage imageWithCGImage:imageRef]; 

這應該然後按預期工作!

+0

與我一樣的答案,但我會從'作物中返回新圖像Image'方法,因此,如果需要,'cropImage'可以重複用於其他圖像(如'BottomImage'或'RotaterImage' :) – deanWombourne