2012-12-26 122 views
2

我想將2個UIImageView中的2個圖像合併爲1個圖像,並在屏幕上顯示「正確」位置,角度,比例比例。如何將2個UIImageView中的2個圖像合併爲1個圖像

在我的項目中,有2的UIImageView - ImageView的是主要的UIImageView - topImageView是覆蓋的UIImageView是可以拖動,旋轉,縮放

我可以保存圖像合併以得出2幅圖像,但錯了位置,角度,比例尺度。

這是我的代碼:

UIImage *img = topImageView.image; 
UIImage *bottomImg = self.imageView.image; 
CGSize bounds = self.imageView.image.size; 

UIGraphicsBeginImageContext(bounds); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(ctx, topImageView.image.size.width * 0.5, topImageView.image.size.height * 0.5); 
CGFloat angle = atan2(topImageView.transform.b, topImageView.transform.a); 
CGContextRotateCTM(ctx, angle); 

[topImageView.image drawInRect:CGRectMake(-topImageView.image.size.width * 0.5, 
              -topImageView.image.size.height * 0.5, 
              topImageView.image.size.width, 
              topImageView.image.size.height)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIGraphicsBeginImageContext(bounds); 
[bottomImg drawInRect:CGRectMake(0, 0, bounds.width, bounds.height)]; 
[newImage drawInRect:CGRectMake(topImageView.frame.origin.x, 
           topImageView.frame.origin.y, 
           newImage.size.width, 
           newImage.size.height)]; 
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

任何想法幫助我嗎?非常感謝!

+0

複製http://stackoverflow.com/questions/9208951/ios-merging-兩圖像 - 的-不同尺寸 –

回答

6

將這兩個圖像作爲子視圖添加到UIView中。你需要的圖片是什麼,然後採取的UIView像這樣的快照:

UIGraphicsBeginImageContext(view.bounds.size); 
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *MergedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
2

嘗試這個

#pragma mark - Marge two Images 

- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2{ 

    CGSize size = CGSizeMake(imageView.image.size.width, imageView.image.size.height); 
    UIGraphicsBeginImageContext(size); 

    CGPoint pointImg1 = CGPointMake(0,0); 
    [img drawAtPoint:pointImg1]; 

    CGPoint pointImg2 = CGPointMake(0,0); 
    [img2 drawAtPoint: pointImg2]; 

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
} 


[self addImageToImage:imageView.image withImage2:imageView2.image]; 
相關問題