2

I want to crop a UIImage that the size of the UIImage is 640*960 and i want to crop it and it will be 640*640.

I try to use this method:

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);]]

and this CGRect : (0,0,640,640) but it give me UIImage that is not 640*640 from the original UIImage

Minakshi
  • 1,437
  • 1
  • 11
  • 19
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • possible duplicate of [UIImage: Resize, then Crop](http://stackoverflow.com/questions/603907/uiimage-resize-then-crop) – Brad Larson Nov 18 '11 at 16:53
  • Check this post here. This does more than resizing. crop, resize, rounded corner are all supported. http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ – Ed Wei Nov 09 '11 at 08:36
  • check the following links. You can get an idea to crop and resize the images. 1. http://stackoverflow.com/questions/158914/cropping-a-uiimage 2. http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ – iOS Nov 09 '11 at 09:09
  • MTA please edit your question and write it properly as your requirement was to resize image not crop image. – Minakshi May 02 '13 at 04:30

1 Answers1

-3

use the following function

        UIImage *newImage = [self imageWithImage:mainDelegate.starImage scaledToSize:CGSizeMake(640, 640)];  

   - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

This will return the image with size (newSize.width,newSize.height)

Minakshi
  • 1,437
  • 1
  • 11
  • 19
  • your answer was really good but it still give me an image that wasn't look good because it's stretch – YosiFZ Nov 10 '11 at 08:31
  • ya it will stretch because you are just resizing the image....I was having same problem but for stretching of image i hadn't got any solution – Minakshi Nov 10 '11 at 11:45
  • -1 Resize is not the same as crop. This should not be checked as answered. – Andres Canella Sep 06 '12 at 02:38
  • Though MTA has used word crop, his/her question was about resizing am image into the new size. And I have understood the question very well, if you can not read and understand the question , you will never able to answer any question. So questioner may not be able to write a question in proper word, I have given a right answer fro it and he/she has accepted it. It is like you have not read question carefully and so this answer should not be voted down. May be you want to vote down an answer or better is you edit the question instead of voting down anyone. Think positive you will see positive. – Minakshi Sep 06 '12 at 12:33
  • @Xyz Thank you, I did read and have now reread carefully and it does refer to "crop" not "resize" in concept and wording. Author is asking to take a 640*960 to 640*640 without stretching, As per his comment on your answer "your answer was really good but it still give me an image that wasn't look good because it's stretch". In this case if you scale it will stretch, if you crop it will not. The answer is not correct to the question, that is what vote down is there for. Im not being negative, Im geared at creating the best content possible. – Andres Canella Sep 15 '12 at 04:36