Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Divide image in equal parts wrt number of row and number of column.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 249
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: