IPhone: the easiest way to crop an image of your desired shape and size

I am trying to crop the image according to my desire .... for my application like kinematics ..... and use it in my application

I tried many options, but none of them ... useful for me, I read the answers to the stack over the stream, but do not use

plzz help me

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

+7
source share
4 answers

Here is the exact code to resize the image to fit

  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 the right size

+11
source

Please find the code below to 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]; 

Link to How to crop an image

+3
source

I think this is the easiest 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 the image; Orientation before cropping; otherwise, you will get a 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; } 
+2
source

Here is a class using Dinesh to get a square thumbnail 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 } 
0
source

All Articles