如何以编程方式将图像切成 4 、 9 、 16 和 25 个切片 [英] How to programmatically slice an image in 4 , 9 , 16 and 25 slices

查看:59
本文介绍了如何以编程方式将图像切成 4 、 9 、 16 和 25 个切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一点帮助以编程方式将图片切成 4 、 9 、 16 和 25 个切片

i need a little help to slice a picture programatically in 4 , 9 ,16 and 25 slices

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

I would appreciate any help or idea . Thanks

推荐答案

我为此制作了一个简单的应用程序.您可以从这里下载:

I made a simple app for this purpose. You can download from here:

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];

结果:

这篇关于如何以编程方式将图像切成 4 、 9 、 16 和 25 个切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆