iOS Color to Transparent in Uiimage

How to make one color transparent on a UIImage?

-(void)changeColor
{
UIImage *temp23=[UIImage imageNamed:@"leaf.png"];
CGImageRef ref1=[self createMask:temp23];
const float colorMasking[6] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0};
CGImageRef New=CGImageCreateWithMaskingColors(ref1, colorMasking);
UIImage *resultedimage=[UIImage imageWithCGImage:New];
}

-(CGImageRef)createMask:(UIImage*)temp
{
CGImageRef ref=temp.CGImage;
int mWidth=CGImageGetWidth(ref);
int mHeight=CGImageGetHeight(ref);
int count=mWidth*mHeight*4;
void *bufferdata=malloc(count);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGContextRef cgctx = CGBitmapContextCreate (bufferdata,mWidth,mHeight, 8,mWidth*4, colorSpaceRef, kCGImageAlphaPremultipliedFirst);

CGRect rect = {0,0,mWidth,mHeight};
CGContextDrawImage(cgctx, rect, ref);
bufferdata = CGBitmapContextGetData (cgctx);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bufferdata, mWidth*mHeight*4, NULL);
CGImageRef savedimageref = CGImageCreate(mWidth,mHeight, 8, 32, mWidth*4, colorSpaceRef, bitmapInfo,provider , NULL, NO, renderingIntent);
CFRelease(colorSpaceRef);
return savedimageref;
}

The above code is tested and I changed the green color to red color by using mask

How to fill with color the transparent part of the UIImage?

For this it would be better to just use the cloud texture as is an then color blend your SKSpriteNode.

https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

Note you'll need to set the colorBlendFactor as well. The default is 0.

https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor

iOS color to transparent in UIImage

UIImage *image = [UIImage imageNamed:@"image.png"];

const float colorMasking[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};
image = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(image.CGImage, colorMasking)];

You receive nil, because parameters, that you send is invalid. If you open Apple documentation, you will see this:

Components
An array of color components that specify a color or range of colors
to mask the image with. The array must contain 2N
values { min[1], max[1], ... min[N], max[N] } where N is the number of
components in color space of image. Each value in components must be a
valid image sample value. If image has integer pixel components, then
each value must be in the range [0 .. 2**bitsPerComponent - 1] (where
bitsPerComponent is the number of bits/component of image). If image
has floating-point pixel components, then each value may be any
floating-point number which is a valid color component.

You can quickly open documentation by holding Option + Mouse Click on some function or class, like CGImageCreateWithMaskingColors.

How to set transparent background color in UIImageView

Set opaque to NO and alpha to 0.3, like this:

UIImageView *myImageView = ...
myImageView.opaque = NO;
myImageView.alpha = 0.3;

How to make one colour transparent in UIImage

From the docs:

components

An array of color components that specify a color or range of colors
to mask the image with. The array must contain 2N values { min1,
max1, ... min[N], max[N] } where N is the number of components in
color space of image. Each value in components must be a valid image
sample value. If image has integer pixel components, then each value
must be in the range [0 .. 2**bitsPerComponent - 1] (where
bitsPerComponent is the number of bits/component of image). If image
has floating-point pixel components, then each value may be any
floating-point number which is a valid color component.

In plain English, if you have a typical RGB image (RGB is the name of the color space), then you have 3 components: R (red), G (green), and B (blue), each one ranging from 0 to 255 (2**8 - 1, assuming 8 bits per component).

So, colorMasking defines the ranges of values for each component that you want to make transparent, ie., the first element in colorMasking is the minimum red component, the second one is the maximum red component, the third one is the minimum green component, and so forth.

The result image will be the input image with some pixels transparent. Which pixels? Those whose RGB values lie between the ranges you set in colorMasking.

In your example, the array is all zeros because you want to make black transparent (and remember, black color in RGB is (0,0,0)).



Related Topics



Leave a reply



Submit