Question: I'm using this code to resize an image taken with the camera. I reduce it proportionally to a thumbnail, but then I'd like to crop it into a 60x60 square format. I tried incorporating:

```
CGImageRef imageRef = CGImageCreateWithImageInRect(CGImage, rect); 

```

but I didn't manage to get it working correctly.How would I do this using Core Graphics?

```
- (UIImage *) resizeCapturedImage:(UIImage *) img toWidth:(float) w{

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL,w,w, 8, 0, colorspace,kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(ctx, CGRectMake(0, 0, w,img.size.width*w/img.size.height), img.CGImage);
    CGImageRef thumb = CGBitmapContextCreateImage(ctx);
    UIImage *final = [UIImage imageWithCGImage:thumb scale:1.0 orientation:UIImageOrientationRight];

    return final;
}

```

Answer: 
Here i put logic of both Crop and resize image, use it as per your requirement.

**For Get Cropped Image:**

```
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need); // set frame as you need
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

```

The following method that return `UIImage` (*as You want size of image*) 

```
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

```

Here you get Croped Image that return by above method;

**OR RESIZING**

And also use following method with specific **hight** and **width** of image for Resizing:

```
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(CGFloat)width withHeight:(CGFloat)height
{
    CGSize newSize = CGSizeMake(width, height);
    CGFloat widthRatio = newSize.width/image.size.width;
    CGFloat heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

```

This method return **NewImage**, with specific size that you want.
Just wanted to point out that for performance it is better to crop the image first and then resize the cropped portion than to resize the entire image and then crop it. Especially for larger images.