#import <UIKit/UIKit.h>
@interface UIImage (NineGrid)
- (NSArray<UIImage *> *)nineGridImages;
@end
@implementation UIImage (NineGrid)
- (NSArray<UIImage *> *)nineGridImages {
NSMutableArray<UIImage *> *images = [NSMutableArray arrayWithCapacity:9];
CGFloat imageWidth = self.size.width / 3.0;
CGFloat imageHeight = self.size.height / 3.0;
CGFloat scale = self.scale;
for (NSInteger row = 0; row < 3; row++) {
for (NSInteger col = 0; col < 3; col++) {
CGRect rect = CGRectMake(col * imageWidth * scale, row * imageHeight * scale, imageWidth * scale, imageHeight * scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
if (imageRef) {
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
[images addObject:croppedImage];
CGImageRelease(imageRef);
}
}
}
return [images copy];
}
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
UIImage *image = [UIImage imageNamed:@"your_image_name.png"];
NSArray<UIImage *> *nineGridImages = [image nineGridImages];
for (UIImage *img in nineGridImages) {
NSLog(@"%@", img);
}
}
}
将一张图片,实现9宫格切割,点击按钮随机打乱顺序,再重新合成一张新图片
~~~
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
@implementation ViewController {
UIImageView *imageView;
UIImage *originalImage;
NSArray<UIImage *> *nineGridImages;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 加载并显示原始图片
originalImage = [UIImage imageNamed:@"your_image_name.png"];
imageView = [[UIImageView alloc] initWithImage:originalImage];
imageView.frame = CGRectMake(50, 50, 300, 300);
[self.view addSubview:imageView];
// 切割图片为九宫格
nineGridImages = [self nineGridImagesFromImage:originalImage];
// 创建按钮并添加到视图
UIButton *shuffleButton = [UIButton buttonWithType:UIButtonTypeSystem];
[shuffleButton setTitle:@"Shuffle" forState:UIControlStateNormal];
shuffleButton.frame = CGRectMake(50, 400, 100, 50);
[shuffleButton addTarget:self action:@selector(shuffleImages) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shuffleButton];
}
- (NSArray<UIImage *> *)nineGridImagesFromImage:(UIImage *)image {
NSMutableArray<UIImage *> *images = [NSMutableArray arrayWithCapacity:9];
CGFloat imageWidth = image.size.width / 3.0;
CGFloat imageHeight = image.size.height / 3.0;
CGFloat scale = image.scale;
for (NSInteger row = 0; row < 3; row++) {
for (NSInteger col = 0; col < 3; col++) {
CGRect rect = CGRectMake(col * imageWidth * scale, row * imageHeight * scale, imageWidth * scale, imageHeight * scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
if (imageRef) {
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
[images addObject:croppedImage];
CGImageRelease(imageRef);
}
}
}
return [images copy];
}
- (void)shuffleImages {
// 打乱九宫格图片的顺序
NSArray<UIImage *> *shuffledImages = [self shuffleArray:nineGridImages];
// 重新合成图片
UIImage *shuffledImage = [self compositeImageFromGridImages:shuffledImages];
// 更新 ImageView 显示新的合成图片
imageView.image = shuffledImage;
}
- (NSArray<UIImage *> *)shuffleArray:(NSArray<UIImage *> *)array {
NSMutableArray<UIImage *> *mutableArray = [array mutableCopy];
NSUInteger count = mutableArray.count;
for (NSUInteger i = 0; i < count; ++i) {
NSUInteger remainingCount = count - i;
NSUInteger exchangeIndex = i + arc4random_uniform((u_int32_t)remainingCount);
[mutableArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
return [mutableArray copy];
}
- (UIImage *)compositeImageFromGridImages:(NSArray<UIImage *> *)gridImages {
CGSize size = originalImage.size;
UIGraphicsBeginImageContextWithOptions(size, NO, originalImage.scale);
CGFloat imageWidth = size.width / 3.0;
CGFloat imageHeight = size.height / 3.0;
for (NSInteger i = 0; i < gridImages.count; i++) {
NSInteger row = i / 3;
NSInteger col = i % 3;
CGRect rect = CGRectMake(col * imageWidth, row * imageHeight, imageWidth, imageHeight);
[gridImages[i] drawInRect:rect];
}
UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return compositeImage;
}
@end
~~