如何在不使用set alpha的情况下合并2个图像?

时间:2012-10-08 09:30:29

标签: iphone image image-processing

我是iPhone的新手开发者。

我希望合并两张图片并在UIImageView中只获取一张图片而不设置alpha。

这是我的代码。此代码使用alpha, 但是我想要没有设置alpha的设置。

mycode的: -

-(UIImage *)maskingImage:(UIImage *)image
{

    CGSize sizeR = CGSizeMake(200, 220);
   // UIImage *textureImage = [UIImage imageNamed:@"tt.png"];
    UIImage *textureImage =imgView2.image;



    UIGraphicsBeginImageContextWithOptions(sizeR, YES, textureImage.scale);
    [textureImage drawInRect:CGRectMake(0.0, 0.0, 200, 220)];

    UIImage *bottomImage = UIGraphicsGetImageFromCurrentImageContext();

    UIImage *upperImage = image;


    CGSize newSize = sizeR ;
    UIGraphicsBeginImageContext(newSize);
    [bottomImage drawInRect:CGRectMake(0.0, 0.0, 200, 220)];
    [upperImage drawInRect:CGRectMake(0.0, 0.0, 200, 220) blendMode:kCGBlendModeNormal alpha:0.5];



    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage; 
}

提前致谢。

3 个答案:

答案 0 :(得分:1)

UIGraphicsBeginImageContext(YOUR SIZE);

//FIRST IMAGE
[FIRST_IMAGE drawInRect:CGRectMake(0, 0, YOUR_SIZE_WIDTH/2, YOUR_SIZE_HEIGHT)];
//SECOND IMAGE
[SECOND_IMAGE drawInRect:CGRectMake(YOUR_SIZE_WIDTH/2, 0, YOUR_SIZE_WIDTH/2, YOUR_SIZE_HEIGHT)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

答案 1 :(得分:1)

使用此功能

- (UIImage * ) mergeImage: (UIImage *) imageA
            withImage:  (UIImage *) imageB
             strength: (float) strength  X:(float )x Y:(float)y{

UIGraphicsBeginImageContextWithOptions(CGSizeMake([imageA size].width,[imageA size].height), NO, 0.0);

[imageA drawAtPoint: CGPointMake(0,0)];

[imageB drawAtPoint: CGPointMake(x,y)
          blendMode: kCGBlendModeNormal // you can play with this
              alpha: strength]; // 0 - 1

UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return mergedImage;}

这里x和y是你想要显示第二张图像的地方

答案 2 :(得分:1)

我刚遇到同样的问题,现在我得到了解决问题的方法

 CGSize newSize = CGSizeMake(320, 377);
    UIGraphicsBeginImageContext( newSize );

    // Use existing opacity as is
    [ image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Apply supplied opacity
    [image2 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];

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

试试这个,它对我来说就像一个魅力,我希望你也能得到解决方案。

你可以使用喜欢 -

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.0];