이미지의 완전히 불투명한(알파값 100%) 픽셀만 추출하는 Objective-C 코드입니다.
파일은 Xcode 프로젝트의 루트에 두면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void) createOpaqueImage:(NSString *)imagePath
{
    UIImage *source = [UIImage imageWithContentsOfFile:imagePath];
   
    CGContextRef ctx;
    CGImageRef imageRef = [source CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char *)malloc(height * width * 4);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast);
   
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
   
    unsigned char *newRawData = (unsigned char *)malloc(height * width * 4);
   
    int byteIndex = 0;
    for (int ii = 0 ; ii < width * height ; ++ii)
    {
        if(((CGFloat)rawData[byteIndex + 3]) >= 255.0f)
        {// 불투명 픽셀
            newRawData[byteIndex] = rawData[byteIndex];
            newRawData[byteIndex + 1] = rawData[byteIndex + 1];
            newRawData[byteIndex + 2] = rawData[byteIndex + 2];
            newRawData[byteIndex + 3] = rawData[byteIndex + 3];
        }
       
        byteIndex += 4;
    }
   
    free(rawData);
   
    ctx = CGBitmapContextCreate(newRawData,
                                width,
                                height,
                                8,
                                bytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedLast );
    CGColorSpaceRelease(colorSpace);
   
    imageRef = CGBitmapContextCreateImage(ctx);
    UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
   
    CGContextRelease(ctx);
    free(newRawData);
   
    // 이미지뷰 생성
    UIImageView *imgView = [[UIImageView new] initWithImage:rawImage];
    [imgView setCenter:CGPointMake(150, 410)];
    [imgView setBounds:CGRectMake(0, 0, width * 0.5, height * 0.5)];
    [self.view addSubview:imgView];
   
    /* 파일 생성
    NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
    NSData *newImg = [NSData dataWithData:UIImagePNGRepresentation(rawImage)];
    [newImg writeToFile:tempFilePath atomically:YES];
   
    rawImage = nil;
   
    return tempFilePath;*/
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"icon1" ofType:@"png"];
   
    // /var/containers/Bundle/Application/086AAB96-982A-4ED5-98F2-C0EC1327D3DB/Test.app/icon1.png
    NSLog(@"%@", path);
   
    UIImage *source = [UIImage imageNamed:path];
    CGImageRef imageRef = [source CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
   
    UIImageView *imgView = [[UIImageView new] initWithImage:source];
    [imgView setCenter:CGPointMake(150, 150)];
    [imgView setBounds:CGRectMake(0, 0, width * 0.5, height * 0.5)];
    [self.view addSubview:imgView];
   
    [self createOpaqueImage:path];
}

@end

사용한 이미지: 초록색 배경은 알파값 90% 입니다.

결과 화면