IOS:如何将 UIImage 拆分为多个部分 [英] IOS: How to split an UIImage into parts

查看:45
本文介绍了IOS:如何将 UIImage 拆分为多个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,我需要将 UIImage 分成多个部分.以下是我用来拆分的代码.我的问题是我无法通过将图像添加到 UIImageView 来加载图像视图.

In one of my application I need to split UIImage into multiple parts. The following was the code I am using to split. Here my problem is I am unable to load the image view by adding the image to UIImageView.

- (void)viewDidLoad
{
    UIImage* image = [UIImage imageNamed:@"monalisa.png"];

    NSMutableArray* splitImages = [self splitImageIntoRects:(__bridge CGImageRef)(image)];
    printf("\n count; %d",[splitImages count]);

    CALayer *layer = [splitImages objectAtIndex:5];

    CGImageRef imgRef = (__bridge CGImageRef)(layer.contents);
    UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];


    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    imageview.image = img;
    imageview.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageview];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (NSMutableArray*)splitImageIntoRects:(CGImageRef)anImage
{
CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));

    NSMutableArray *splitLayers = [NSMutableArray array];

   int kXSlices = 3;
   int kYSlices = 3;

    for(int x = 0;x < kXSlices;x++) {
        for(int y = 0;y < kYSlices;y++) {
            CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
                                      (imageSize.height / kYSlices) * y,
                                      (imageSize.width / kXSlices),
                                      (imageSize.height / kYSlices));

            CALayer *layer = [CALayer layer];
            layer.frame = frame;
            CGImageRef subimage = CGImageCreateWithImageInRect(anImage, frame);
            layer.contents = (__bridge id)subimage;
            [splitLayers addObject:layer];
        }
    }
    return splitLayers; 
}

输出如下,

推荐答案

试试这个:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getSplitImagesFromImage:[UIImage imageNamed:@"Image1.png"] withRow:4 withColumn:4];
}

-(NSMutableArray *)getSplitImagesFromImage:(UIImage *)image withRow:(NSInteger)rows withColumn:(NSInteger)columns
{
    NSMutableArray *aMutArrImages = [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 aIntY = 0; aIntY < columns; aIntY++)
    {
        xPos = 0.0;
        for (int aIntX = 0; aIntX < rows; aIntX++) 
        {                
            CGRect rect = CGRectMake(xPos, yPos, width, height);
            CGImageRef cImage = CGImageCreateWithImageInRect([image CGImage],  rect);

            UIImage *aImgRef = [[UIImage alloc] initWithCGImage:cImage];
            UIImageView *aImgView = [[UIImageView alloc] initWithFrame:CGRectMake(aIntX*width, aIntY*height, width, height)];
            [aImgView setImage:aImgRef];
            [aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
            [aImgView.layer setBorderWidth:1.0];
            [self.view addSubview:aImgView];

            [aMutArrImages addObject:aImgRef];
            xPos += width;
        }
        yPos += height;
    }
    return aMutArrImages;
}

欲了解更多信息见这个,您也可以从此处下载演示.

for more info see this and you can also download demo from here.

这篇关于IOS:如何将 UIImage 拆分为多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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