2012-08-09 33 views
0

的大小,所以我有這樣的功能:改變一個UIImage

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

    UIImageView *temp_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    temp_img.image = secondImage; 
    secondImage = temp_img.image; 



    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((100)), 
             roundf((100)))]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 

我試着去改變secondImage的UIImage的大小,這樣,當他們合併,我可以調整大小,使它看起來不錯。

任何幫助表示讚賞謝謝你們!

回答

1

注意在UIGraphicsBeginImageContextWithOptions的「0」:

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 
CGContextConcatCTM(context, flipVertical); 
... 
// make anySize the size you want and pt any point 

CGContextDrawImage(context, (CGRect){ pt1, anySize1 }, [first CGImage]); 
// make anySize the size you want and pt any point 
CGContextDrawImage(context, (CGRect){ pt2, anySize2 }, [secondImage CGImage]); 

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

return image; 
+0

我試過,但形象只是出現倒掛......你能更新與全功能您的文章?第二個圖像是垂直翻轉的......我嘗試了一些方法將其翻轉,但它似乎沒有工作。 – jimbob 2012-08-10 15:42:32

+0

我忘了一行 - 現在添加它:\t \t CGContextConcatCTM(context,flipVertical); – 2012-08-10 16:52:52

+0

工作感謝! – jimbob 2012-08-10 17:02:03