2012-06-14 57 views
2

我需要兩個圖像合併成一個圖像,這裏是我的代碼:合併兩個圖像轉換成一個圖像iPhone

-(UIImage*)mergeImage:(UIImage*)mask overImage:(UIImage*)source inSize:(CGSize)size 
{ 
    //Capture image context ref 

    UIImageView *totalimage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; 

    UIImageView *firstImage=[[UIImageView alloc] initWithImage:mask]; 
    UIImageView *secondImage=[[UIImageView alloc] initWithImage:source]; 

    [totalimage addSubview:firstImage]; 
    [totalimage addSubview:secondImage]; 

    UIGraphicsBeginImageContext(totalimage.bounds.size); 
    [totalimage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //Draw images onto the context 
    [source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
    [mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)]; 

    return viewImage; 

} 

我把這種方法如下:

UIImage *totalImage = [self mergeImage:self.Apicimage overImage:questionImage inSize:CGSizeMake(100, 100)]; 

但在執行我得到這個輸出:

Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSaveGState: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetBlendMode: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextSetAlpha: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextTranslateCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextScaleCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextConcatCTM: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextDrawImage: invalid context 0x0 
Jun 15 16:11:06 iPad-2 VueGuides[9195] <Error>: CGContextRestoreGState: invalid context 0x0 

任何人都可以指導我。我怎樣才能合併兩個圖像?

+0

寫,你實際上並沒有使用你通過你的'CGSize'參數。 – Nate

回答

3

代碼

[source drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
    [mask drawInRect:CGRectMake(0, 0, mask.size.width, mask.size.height)]; 

前應

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
除了ArunGJ的答案,這是正確的