IOS: How to split an UIImage into parts

青春壹個敷衍的年華 提交于 2019-12-08 04:13:53

问题


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

And the output is like follows,


回答1:


Try This:

- (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.




回答2:


We can enhance more the Yasika Patel Answer. Below function will give you exact peice of image which fits to your view.

- (void)splitImage :(UIImage *)image withColums:(int)columns WithRows:  (int)rows ViewToIntegrate : (UIView *)view
 {
     CGSize imageSize = _imgSplit.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*(view.frame.size.width/rows), aIntY*( view.frame.size.height/columns), view.frame.size.width/rows, view.frame.size.height/columns)];

            [aImgView setImage:aImgRef];
            [aImgView.layer setBorderColor:[[UIColor blackColor] CGColor]];
            [aImgView.layer setBorderWidth:0.5];
            [view addSubview:aImgView];
            xPos += width;
        }
        yPos += height;
     }

   [self.view addSubview:view];
}

This will give you the image in 9parts . here you just need to pass the row and colums.



来源:https://stackoverflow.com/questions/25607688/ios-how-to-split-an-uiimage-into-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!