안녕하세요.
아이폰에서 uiimage를 특정 픽셀을 투명하게 하고싶은데요.
안드로이드에서는 알파값에만 0주면 투명하게되었는데
아이폰에서는 R,G,B,A 다 0값을줘야 해당픽셀이 투명하게 되더라구요..
기초지식이없어서 왜이런지 이해가 잘안되는 상황인데.
알파값만 0줘도 투명하게 하는방법 아시는분 있나요. 사진은 샘플로 쓰고 있는 코드입니다.
- (UIImage *)processWithePixels:(UIImage*)teeImage alpha:(int)trans{
UInt32 * teePixels;
CGImageRef teeCGImage = [teeImage CGImage];
NSUInteger teeWidth = CGImageGetWidth(teeCGImage);
NSUInteger teeHeight = CGImageGetHeight(teeCGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;
NSUInteger teeBytesPerRow = bytesPerPixel * teeWidth;
teePixels = (UInt32 *)calloc(teeHeight * teeWidth, sizeof(UInt32));
CGContextRef context = CGBitmapContextCreate(teePixels, teeWidth, teeHeight,
bitsPerComponent, teeBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, teeWidth, teeHeight), teeCGImage);
for (NSUInteger j = 0; j < teeHeight; j++) {
for (NSUInteger i = 0; i < teeWidth; i++) {
UInt32 * currentPixel = teePixels + ( j * teeWidth) + i;
UInt32 color = *currentPixel;
int mColor = 0;
if(color != 0){
if(B(color) < R(color)){
mColor = B(color);
}else{
mColor = R(color);
}
if(mColor > G(color)){
mColor = G(color);
}
mColor = 255 - mColor;
mColor = (int)(mColor * 2);
if(mColor > trans ){
mColor = trans;
}
}else{
mColor = 0;
}
CGFloat alpha = 255 - mColor;
UInt32 newR = R(color) - alpha;
UInt32 newG = G(color) - alpha;
UInt32 newB = B(color) - alpha;
UInt32 newA = A(color) - alpha;
newR = MAX(0,MIN(255, newR));
newG = MAX(0,MIN(255, newG));
newB = MAX(0,MIN(255, newB));
*currentPixel = RGBAMake(newR, newG, newB, mColor);
}
}
// 4. Create a new UIImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage scale:1 orientation:teeImage.imageOrientation];
// 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CFRelease(newCGImage);
free(teePixels);
return processedImage;
};