我需要一点帮助,用程序将一张图片切成4、9、16和25片。
我会感谢任何帮助或想法。谢谢
发布于 2014-11-24 13:55:09
我为此做了一个简单的应用程序。您可以从这里下载:
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];结果:

发布于 2014-11-24 13:57:17
https://stackoverflow.com/questions/27106391
复制相似问题