With the help of following code you can divide UIImage into equal parts w.r.t number of row and number of column.
Create a category of UIImageView and UImage and write the following method respectively in them.
-(NSArray *)divideImageViewInImage:(int)row column:(int)column{
NSMutableArray *imageArray = [NSMutableArray new];
CGFloat width,height;
width = self.frame.size.width/column;
height = self.frame.size.height/row;
CGFloat x = 0,y = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
CGRect frame = CGRectMake(x, y, width, height);
[imageArray addObject:[self.image imageFromImageView:self rectFrame:frame]];
x = x + width;
}
x = 0;
y = y + height;
}
return imageArray;
}
-(UIImage *)imageFromImageView:(UIImageView *)imageView rectFrame:(CGRect)frame{
UIGraphicsBeginImageContextWithOptions(frame.size, imageView.opaque, 2.0);
// UIGraphicsBeginImageContext(frame.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextConcatCTM(c, CGAffineTransformMakeTranslation(-frame.origin.x, -frame.origin.y));
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Happy coding :)
0 Comment(s)