아이폰에서 uiimage 픽셀에서 특정 픽셀들 투명하게 하는방법
조회수 2359회
안녕하세요.
아이폰에서 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;
};
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
kCGImageAlphaPremultipliedLast
이 부분과 연관이 있을것 같습니다. Alpha channel이 비트맵의 가장 뒤에 있는 RGBA형 이미지를 가정하고 이미지를 읽어오고, RGBA로 쓰고 있으신데요.
요즘 버전에서는 ARGB를 쓴다고 하네요. 혹시 R만 0으로 해도 투명해 진다면 ARGB와 RGBA사이의 차이에서 오는 문제이겠네요.
추가 내용
kCGBitmapByteOrder32Big에도 영향을 받습니다. little endian인지 Big endian인지에 따라 달라지는것 같은데요.
우선
CGImageGetBitmapInfo info = CGImageGetBitmapInfo(teeCGImage)
해서 어떤 비트맵 타입인지를 읽어온 다음에 알맞는 context를 선택해 주셔야 겠습니다. 여기에 CGImageGetBitmapInfo 관련정보가 있으니 참고해 보세요.- 정두식님 답변감사합니다. kkokkokim14 2016.4.22 09:19
- 정두식님 답변감사합니다. 저두 그런거 때문에 CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big; 속성을 주고 아예 픽섹을 0x00FFFFFF을 줘보았는데도 안되더라구요 ㅜㅜ kkokkokim14 2016.4.22 09:20
- 답변에 내용을 추가했습니다. 정토드 2016.4.22 10:01
- 답변감사합니다 현재 보니까 kkokkokim14 2016.4.22 11:40
- 답변감사합니다 현재 보니까 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 이것이 문제 인거같은데요 *currentPixel = 0x00FF00FF; 이것을 무조건 이미지에 넣고있는데 투명하게 안되더라구요 CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);이것을 넣으면 00값이되면 투명은되는데 10이나 넣으면 rgb값에서 투명이 흰색쪽으로 색이 변하면서 불투명이 되더라구요 kkokkokim14 2016.4.22 11:41
댓글 입력