改变UIImage的大小

时间:2012-08-09 21:31:20

标签: objective-c ios cocoa-touch 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 个答案:

答案 0 :(得分: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;