主页 UIImage Mirroring
Post
Cancel

UIImage Mirroring

Preface

This article is highly personal in tone. If you find it uncomfortable to read, please close it as soon as possible. This article is only a personal learning record. Reposts and sharing are welcome within the scope of the license agreement. Please respect the copyright and keep the original link. Thank you for your understanding and cooperation. If you find this site helpful, you can subscribe via RSS. Thanks for your support!

Example Code Record

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
@implementation UIImage (MTMirrorImage)

- (nullable UIImage *)mirrorImageHorizontally
{
    if (self == nil) { return nil; }
    CGSize size = self.size;
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    // Create a bitmap context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *bytes = (unsigned char *)malloc(size.width * size.height * 4);
    CGContextRef context = CGBitmapContextCreate(bytes, size.width, size.height, 8, size.width * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
    // Draw the original image into the bitmap context
    CGContextDrawImage(context, rect, self.CGImage);
    // Flip the image horizontally in the bitmap context
    CGContextTranslateCTM(context, size.width, 0);
    CGContextScaleCTM(context, -1, 1);
    CGContextDrawImage(context, rect, self.CGImage);
    // Get the new image from the bitmap context
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    // Release resources
    CGImageRelease(newImageRef);
    CGContextRelease(context);
    free(bytes);
    CGColorSpaceRelease(colorSpace);
    return newImage;
}

- (nullable UIImage *)mirrorImageVertically;
{
    if (self == nil) { return nil; }
    CGSize size = self.size;
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    // Create a bitmap context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *bytes = (unsigned char *)malloc(size.width * size.height * 4);
    CGContextRef context = CGBitmapContextCreate(bytes, size.width, size.height, 8, size.width * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
    // Draw the original image into the bitmap context
    CGContextDrawImage(context, rect, self.CGImage);
    // Flip the image vertically in the bitmap context
    CGContextTranslateCTM(context, 0, size.height);
    CGContextScaleCTM(context, 1, -1);
    CGContextDrawImage(context, rect, self.CGImage);
    // Get the new image from the bitmap context
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    // Release resources
    CGImageRelease(newImageRef);
    CGContextRelease(context);
    free(bytes);
    CGColorSpaceRelease(colorSpace);
    return newImage;
}

@end
该博客文章由作者通过 CC BY 4.0 进行授权。