how to crop image on camera in iphone? - iphone

Hi am using Camera Application. I need to crop image in particular area of image, which has been taken and used by UIImagepicker. I done with adding overlay in camera and croping image. but i got cropped image is not for my Rect size.

Try using the UIImage+ProportionalFill category by Matt Gemmell. This category extends UIImage by cropping/scaling capabilities.

Croping start from x=0; y=0; and till width=125 and height=128;
UIImage *image =imageView.image;
CGRect cropRect = CGRectMake(0, 0, 125,128);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
[imageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);

Related

Cropping UIImage in any image resolution [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cropping a UIImage
I saw many tutorials about cropping images and am trying my luck but with no success.
I need to crop the image from AVFoundation. Since when the image is being taken from the camera it is rotated left from Portrait mode, I also need to rotate it right and my x and y are opposite. The problem is that if I send the frame of the image where I would it to reside, it seems to me that there is no correlation in the size of the image and the rectangle.
The code is:
#property (weak, nonatomic) IBOutlet UIView *videoPreviewView;
...
...
int width = videoPreviewView.frame.size.width;
int height = videoPreviewView.frame.size.height;
int x = videoPreviewView.frame.origin.x;
int y = videoPreviewView.frame.origin.y;
CGRect croprect = CGRectMake(y, x,height,width);
// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], croprect);
// Create new cropped UIImage
UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:[sourceImage scale] orientation:UIImageOrientationRight];
When I print the size of the frame I get:
(4.5,69.5,310,310)
and the image size is:
(720,1280)
How can I perform cropping in any image resolution?
I tried multiplying the values with image.scale - however, the value is 1.00
Try this one.This will definitely help you out. https://github.com/barrettj/BJImageCropper
To resize image, Try this
UIImage *image = [UIImage imageNamed:#"image.png"];
CGSize itemSize = CGSizeMake((your Width), (your Height));
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
UIImage * yourCroppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
To Crop,
// Create new image context
UIGraphicsBeginImageContext(SIZE);
// Create CGRect for image
CGRect newRect = CGRectMake(x, y,SIZE.width,SIZE.height);
// Draw the image into the rect
[ORIGINALIMAGE drawInRect:newRect];
// Saving the image, ending image context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

how to crop some portion of scaled, rotated image-view

I am unable to crop he image from image view, which is scaled and rotated before cropping. used existing cropping methods.but its applicable only for image-view which is fixed.Hope code will helpful.
Finally succeeded cropping rotated scaled image.
1) Need to crop the entire scaled or rotated image.
2)crop the rect from cropped Image. you will get final scaled or rotated cropped image.
Im posting sample code , it may helpful for others.
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = CGRectMake(100,100 ,200, 200);//Rect we need from scaled or rotated image.
CGImageRef cropped = CGImageCreateWithImageInRect(viewImage.CGImage, rect);
UIImage *img = [UIImage imageWithCGImage:cropped];
CGImageRelease(cropped);
Thanks.
Try This(if you need to crop the corners):
UIImageView *imageSubview = [[UIImageView alloc] initWithFrame:CGRectMake(50.0f, 5.0f,80.0f, 60.0f)];
[imageSubview setImage:actualImage];
[imageSubview setContentMode:UIViewContentModeScaleToFill];
[imageSubview.layer setCornerRadius:10.0];
[imageSubview.layer setMasksToBounds:YES];
[cell addSubview:imageSubview];
Just increase the radius and you will make it circle, or walk on this path you'll get your actual solution.:)
UIImage *myImage = [UIImage imageNamed:#"photo.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
its for cropping particular height and width

UIImageWriteToSavedPhotosAlbum creates black thumbnail in camera roll

I am saving image in the camera roll using UIImageWriteToSavedPhotosAlbum but always get an black thumbnail even if the picture is correct.
Do you have pointers to address this?
Thanks in advance for your help.
Regards,
I had the same problem. Drawing the image within a UIGraphicsImageContext solves the issue:
CGRect rect = CGRectMake(0,0,100,100);
UIImage *image = ((put here your image));
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(
result, self, #selector(image:didFinishSavingWithError:contextInfo:),nil);

How to show specific area in UIImageView from iPhone?

If I have a image size 1024*768, but I want to show a 100*100 square located on 200, 200. What is the simplest way to do it? Thz u.
You're basically talking about a "Crop" operation.
From HitScan in this question:
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);

create UIImageView with portion of image file

I'm subclassing UIImageView to create a tile-based application. Essentially, I am taking a single image file and breaking it up into pieces, and then assigning the pieces to my tiles (UIImageViews), so that they can be manipulated independently.
What's the best way to grab a portion of an image and use that to draw a UIImageView? I thought about overriding drawRect and using CGAffineTransform, but it seems like there ought to be a simpler way to do this, perhaps by specifying a CGRect to the UIImage that is passed to the UIImageView, but I don't see an API for this.
Here we go:
UIImage* img = [UIImage imageNamed:#"myImage.jpg"];
CGRect imgFrame = CGRectMake(x, y, tileWidth, tileHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], imgFrame);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);