开发者

merging a stretchable UIImage with a "normal" one

I'd like to combine two UIImages, one stretchable and one "normal" one. The problem is that if I merge the Images using the UIGraphicsImageContext, the scond image is also stretched (it is on top of the first one as it should be, but stretched). Does anybody know how to avoid this?

Thanks a lot!

calls from my ViewController:

UIImage *stretchImage = [[UIImage imageNamed:@"stretchableLeft.png"]   stretchableImageWithLeftCapWidth:0.0 topCapHeight:16.0];
    stretchImage = [self imageWithImage:stretchImage scaledToSize:CGSizeMake(64.0, 64.0)];
    stretchImage = [self mergeImageWithImage:stretchImage secondImage:[UIImage imageNamed:@"topImage.png"]]; // only 40x40 Px

the two methods are:

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize  
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;


- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image.size.width,image.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newI开发者_开发问答mage;
}


I think the issue is that you're asking both images to draw in the full rectangle. That is causing your second image to stretch.

Try using the image2.size for image2 when merging the images. You'll have to adjust the placement using the x/y coordinate when drawing the rectangle.

- (UIImage*)mergeImageWithImage:(UIImage *)image secondImage:(UIImage *)image2 
{
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
[image2 drawInRect:CGRectMake(10,10,image2.size.width,image2.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜