2014-11-01 58 views
1

我有兩個包含兩個不同圖像的imageView。我的firstImageview有主圖像,然後我添加了一個更多的imageview作爲firstImageview的子視圖,如[firstImageview addSubView:secondImageview]。我需要這個在一個單一的形象。將兩幅圖像合併爲單幅圖像,然後頂部圖像在ios sdk中變大

我使用下面的代碼(它工作順利):

- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage { 
    UIImage *image = nil; 

    CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height)); 
    if (UIGraphicsBeginImageContextWithOptions != NULL) { 
     UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]); 
    } else { 
     // UIGraphicsBeginImageContext(newImageSize); 
    } 
    [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2), 
             roundf((newImageSize.height-firstImage.size.height)/2))]; 
    [secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2), 
             roundf((newImageSize.height-secondImage.size.height)/2))]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 

但在我resultedImage,topImage secondImageview.image變大。

我的觀點看起來像(前合併):

image prior to merge

我造成的圖像合併後(上面的圖像變大):

image after merge

如何合併這些圖像,而不secondImageview變大?

回答

0

我解決我的問題,通過下面的代碼

UIGraphicsBeginImageContextWithOptions(mainImageview.bounds.size, mainImageview.opaque, 0.0); 
[self.mainImageview.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();