아이폰에서 uiimage 픽셀에서 특정 픽셀들 투명하게 하는방법

조회수 2338회

안녕하세요.

아이폰에서 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;
};
  • 코드는 이미지로 올리지 마시고 ```를 이용해서 텍스트로 올려주세요. 정토드 2016.4.21 14:02
  • 죄송합니다. 처음이용해봐서요 kkokkokim14 2016.4.21 14:51
  • 네. 수정 감사합니다. 여전히 조금 이상한 부분이 있어서 편집요청을 보내 놓았어요. 정토드 2016.4.21 16:10
  • 수정하였습니다. 저렇게 하는게 맞나요^^ kkokkokim14 2016.4.21 16:13

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

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)