7

i am trying to crop image according to my desire....for my cinemagram type app.....and use it in my application

i have tried a lot of options but none of tham is...usefull for me i read answers on stack over flow but of no use

plzz help me out

image = [UIImage imageNamed:@"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];

CGSize size = [image size];

[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];    

thanks

swetank
  • 292
  • 1
  • 4
  • 17

4 Answers4

11

Here is the exact code to resize your image to desired size

    CGSize itemSize = CGSizeMake(320,480); // give any size you want to give

    UIGraphicsBeginImageContext(itemSize);

    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);

    [myimage drawInRect:imageRect];


    myimage = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();

Now my image is of your desired size

Kyle
  • 21,377
  • 37
  • 113
  • 200
Dinesh Kaushik
  • 2,917
  • 2
  • 23
  • 36
3

Please find the code below to the crop the image.

// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// Get size of current image
CGSize size = [image size];

// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];    

// Create rectangle that represents a cropped image  
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4 , 
    (size.width / 2), (size.height / 2));

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];

Referred from How to Crop an Image

Akhilesh Sharma
  • 1,580
  • 1
  • 17
  • 29
2

I think this is the most simple way:

CGRect rect = CGRectMake(0.0, 0.0, 320.0, 430.0);
CGImageRef tempImage = CGImageCreateWithImageInRect([originalImage CGImage], rect);
UIImage *newImage = [UIImage imageWithCGImage:tempImage];
CGImageRelease(tempImage);

But don't forget to clear imageOrientation BEFORE cropping, otherwise you will get rotated image after cropping:

-(UIImage *)normalizeImage:(UIImage *)raw {
    if (raw.imageOrientation == UIImageOrientationUp) return raw;
    UIGraphicsBeginImageContextWithOptions(raw.size, NO, raw.scale);
    [raw drawInRect:(CGRect){0, 0, raw.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}
0

Here is a class using Dinesh's answer to get a square thumbnail of an image

class func returnSquareCroppedImage(theImage:UIImage)->UIImage{

    var mySize:CGFloat = 100.0
    var imageRect:CGRect

    if theImage.size.width > theImage.size.height {

        mySize = theImage.size.height
        let xAdjust = 0-((theImage.size.width - theImage.size.height)/2)
        imageRect = CGRectMake(xAdjust, 0, theImage.size.width, theImage.size.height)
    }else{

        mySize = theImage.size.width
        let yAdjust = 0-((theImage.size.height - theImage.size.width)/2)
        imageRect = CGRectMake(0, yAdjust, theImage.size.width, theImage.size.height)
    }

    UIGraphicsBeginImageContext(CGSizeMake(mySize, mySize))

    theImage.drawInRect(imageRect)

    let returnImage = UIGraphicsGetImageFromCurrentImageContext()

    return returnImage
}
PJeremyMalouf
  • 613
  • 6
  • 15