10

我是 iPhone 的新手开发人员。

我想合并两个图像并只获取一个图像UIImageView而不设置 alpha。

这是我的代码。此代码使用 alpha 工作,但我想设置不设置 alpha。

我的代码:-

-(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; 
}

提前致谢。

4

3 回答 3

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();
于 2012-10-08T12:22:48.380 回答
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 是您要显示第二张图片的位置

于 2012-10-09T10:46:56.043 回答
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];
于 2012-10-10T06:45:00.957 回答