如何以编程方式切片4,9,16和25片的图像

我需要一些帮助,以4,9,16和25片的方式编程图片

我将不胜感激任何帮助或想法。 谢谢

我为此做了一个简单的应用程序。 你可以从这里下载:

https://github.com/bpolat/Image-Slicer

您需要的代码是:

 -(NSMutableArray *)getImagesFromImage:(UIImage *)image withRow:(NSInteger)rows withColumn: (NSInteger)columns { NSMutableArray *images = [NSMutableArray array]; CGSize imageSize = image.size; CGFloat xPos = 0.0, yPos = 0.0; CGFloat width = imageSize.width/rows; CGFloat height = imageSize.height/columns; for (int y = 0; y < columns; y++) { xPos = 0.0; for (int x = 0; x < rows; x++) { CGRect rect = CGRectMake(xPos, yPos, width, height); CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage], rect); UIImage *dImage = [[UIImage alloc] initWithCGImage:cImage]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x*width, y*height, width, height)]; [imageView setImage:dImage]; [imageView.layer setBorderColor:[[UIColor blackColor] CGColor]]; [imageView.layer setBorderWidth:1.0]; [self.view addSubview:imageView]; [images addObject:dImage]; xPos += width; } yPos += height; } return images; } 

用法:

 [self getImagesFromImage:[UIImage imageNamed:@"1.png"] withRow:4 withColumn:4]; 

结果:

在这里输入图像说明

这里是一个例子:

 CGRect rect = CGRectMake(size.width / 4, size.height / 4 , (size.width / 2), (size.height / 2)); CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect); UIImage *img = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); 

并检查出这些链接: 裁剪和调整图像的 CGImage参考