从UIImage的除去alpha通道

问题描述 投票:5回答:3

我用下面的方法来获得从文件系统中的解压缩的UIImage。然而,当我把颜色混合层上,即使的UIImageView设置为不透明的UIImageView被标记为红色。

文件系统上的图像没有alpha通道。我试图设置CGContextSetAlpha(bitmapContext,1),但仍然已经共混层。

任何人都知道如何使用CGContextDrawImage时取出alpha通道?

- (UIImage *)decompressedImage
{
    CGImageRef imageRef = self.CGImage;
    CGRect rect = CGRectMake(0.f, 0.f, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
                                                       );
    // kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little are the bit flags required
    //  so that the main thread doesn't have any conversions to do.

    CGContextDrawImage(bitmapContext, rect, imageRef);

    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext);
    UIImage* decompressedImage = [UIImage imageWithCGImage:decompressedImageRef
                                                     scale:[[UIScreen mainScreen] scale]
                                               orientation:UIImageOrientationUp];
    CGImageRelease(decompressedImageRef);
    CGContextRelease(bitmapContext);

    return decompressedImage;
}
ios uiimageview uiimage alpha cgcontext
3个回答
5
投票

在该范围内的选项,请尝试选择

kCGImageAlphaNoneSkipLast

要么

kCGImageAlphaNoneSkipFirst

根据如果你正在使用RGBA或ARGB

CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Little
                                                       );

1
投票

同样的结果,但更少的代码,事情发愁:

func removeAlpha(from inputImage: UIImage) -> UIImage {
        let format = UIGraphicsImageRendererFormat.init()
        format.opaque = true //Removes Alpha Channel
        format.scale = inputImage.scale //Keeps original image scale.
        let size = inputImage.size
        return UIGraphicsImageRenderer(size: size, format: format).image { _ in
            inputImage.draw(in: CGRect(origin: .zero, size: size))
        }
    }

-7
投票
-(void)JhagRemoveMethodRepet
{

    switch (Jhagtag)
    {
        case 1:
        {

            if (jhag1.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag1.alpha;
                jhag1.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:0 withObject:@"Mid"];
            }else
            {
            jhag1.alpha=0.0;
            [JRemovetimer invalidate];
             JRemovetimer=nil;
            [Checkjhag replaceObjectAtIndex:0 withObject:@"No"];
            [self checkcompeletewater];
            }

        }
            break;
        case 2:
        {

            if (jhag2.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag2.alpha;
                jhag2.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:1 withObject:@"Mid"];

                if (jhagBrush==YES)
                {
                    BTooth2.alpha=0.0;
                }
            }else
            {
                jhag2.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:1 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 3:
        {

            if (jhag3.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag3.alpha;
                jhag3.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:2 withObject:@"Mid"];
            }else
            {
                jhag3.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:2 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 4:
        {
            if (jhag4.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag4.alpha;
                jhag4.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:3 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    BTooth3.alpha=0.0;
                    SpiderMud.alpha=0.0;
                }
            }else
            {
                jhag4.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:3 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 5:
        {

            if (jhag5.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag5.alpha;
                jhag5.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:4 withObject:@"Mid"];
            }else
            {
                jhag5.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:4 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 6:
        {
            if (jhag6.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag6.alpha;
                jhag6.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:5 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    GreenMud.alpha=0.0;
                }
            }else
            {
                jhag6.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:5 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 7:
        {
            if (jhag7.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag7.alpha;
                jhag7.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:6 withObject:@"Mid"];
            }else
            {
                jhag7.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:6 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 8:
        {
            if (jhag8.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag8.alpha;
                jhag8.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:7 withObject:@"Mid"];

                if (jhagBrush==YES)
                {
                    BTooth1.alpha=0.0;
                }
            }else
            {
                jhag8.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:7 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 9:
        {
            if (jhag9.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag9.alpha;
                jhag9.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:8 withObject:@"Mid"];
            }
            else
            {
                jhag9.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:8 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 10:
        {
            if (jhag10.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag10.alpha;
                jhag10.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:9 withObject:@"Mid"];
            }else
            {
                jhag10.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:9 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 11:
        {
            if (jhag11.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag11.alpha;
                jhag11.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:10 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    Yellowmud.alpha=0.0;
                }
            }else
            {
                jhag11.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:10 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;

        default:
            break;
    }

}

-(void)checkcompeletewater
{
    NSMutableArray *compeletearr=[NSMutableArray arrayWithObjects:@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No", nil];


    if ([Checkjhag isEqualToArray:compeletearr] && jhagBrush==YES)
    {
        waterClean=YES;
        [self SoundPlay:@"puzzle_right"];
© www.soinside.com 2019 - 2024. All rights reserved.