Search code examples
iosobjective-cuiimagecrop

Crop and recreate UIImage


I want to crop a UIImage (not imageview), before doing pixel operations on it. Is there a reliable way to do this in ios framework?

Here are the related methods in android I am using:

http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap, int, int, int, int) http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(int, int, android.graphics.Bitmap.Config)


Solution

  • This worked for me:

    -(UIImage *)cropCanvas:(UImage *)input x1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2{
        CGRect rect = CGRectMake(x1, y1, input.size.width-x2-x1, input.size.height-y2-y1);
        CGImageRef imageref = CGImageCreateWithImageInRect([input CGImage], rect);
        UIImage *img = [UIImage imageWithCGImage:imageref];
        return img;
    }