I am creating an application in which i am displaying one .jpg image. I want to crop part of image in circular shape. Please help me to solve this problem.
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];
Please tell me to crop part of image in circular shape
You can do it by using Quartz Core framework it really has some cool Apis to do that. Check this RoundedImageView example.
Methinks this is a duplicate. There's an excellent accepted answer in this question, and links to other articles: How to crop UIImage on oval shape or circle shape?
EDIT: There's a handful of easy ways of going about this. A CALayer with a cornerRadius being obvious. But more importantly, there exists the method CGImageCreateWithMask: which can be applied to a broader spectrum of up to and including circles and other shapes. Note that if your image is a JPEG, then CGImageCreateWithMask will return a black background because JPEG's have no alpha channel.
You can use RSKImageCropper for crop the image in circular shape. I am implemented the fallowing code to crop the image in circular shape with the help of RSKImageCropper.
1. Install the pod RSKImageCropper.
2. #import <RSKImageCropper/RSKImageCropper.h> in your viewcontroller
3. Add delegate to your interface i.e. RSKImageCropViewControllerDelegate
4. Implement the fallowing code in **didFinishPickingMediaWithInfo** delegate.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:
^{
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:originalImage];
imageCropVC.avoidEmptySpaceAroundImage = YES;
imageCropVC.delegate = self;
[self presentViewController:imageCropVC animated:NO completion:nil];
}];
}
5. Now implement the delegate of RSKImageCropper.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
[controller dismissViewControllerAnimated:NO completion:nil];
}
// The original image has been cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
{
self.imgVIew.image = croppedImage;
[self.navigationController popViewControllerAnimated:YES];
}
// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
rotationAngle:(CGFloat)rotationAngle
{
self.imgVIew.image = croppedImage;
[controller dismissViewControllerAnimated:NO completion:nil];
}
For more info check this https://github.com/ruslanskorb/RSKImageCropper
Related
I am trying to take picture from camera in landscape mode and I want to show that image in portrait mode in iOS6. I can't rotate the image in portrait mode.
You want to rotate, then what you are gonna to do with rotated image?
If you want to just show it in view, wrap it in UIImageView, rotate imageView and insert into view:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* image = [[info objectForKey: UIImagePickerControllerOriginalImage] fixOrientation];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.transform = CGAffineTransformMakeRotation(M_PI * 90 / 180);
[self.view addSubview:imageView];
}
I wonder, how could I display UIProgressView in this case:
I do photo with UIImagePickerController then in method - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
I save the picture to UIImage object (outputImage) and hide controller, and when modalView/controller with camera is disappeared I have such code:
NSLog(#"Image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//FLIP PICTURE FROM FACETIME CAMERA
if((outputImage.size.width == 480 && outputImage.size.height == 640) || (outputImage.size.width == 640 && outputImage.size.height == 480)) {
outputImage = [UIImage imageWithCGImage:outputImage.CGImage
scale:1.0 orientation: UIImageOrientationLeftMirrored];}
//RESIZE TO 1000x1000
outputImage = [self scaleAndCropToSize:outputImage toSize:CGSizeMake(1000, 1000)];
NSLog(#"Now image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//CUTING OUT TO MASK
outputImage = [self maskImage:outputImage withMask:[UIImage imageNamed:#"mask.png"]];
//ADDING SHADOW
CGSize size = CGSizeMake(1000, 1000);
UIGraphicsBeginImageContext(size);
CGPoint thumbPoint = CGPointMake(0, 0);
[outputImage drawAtPoint:thumbPoint];
UIImage* shadow = [UIImage imageNamed:#"shadow.png"];
CGPoint starredPoint = CGPointMake(0, 0);
[shadow drawAtPoint:starredPoint];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
outputImage = result;
//ADDING TO UIIMAGEVIEW (PREVIEW)
preview.image = outputImage;
While photo is being put into UIImageView, it takes some time. In this time I'd like to display progress view. How can I do that?
EDIT: I thought to place above code in some method and then somehow implement progressview, that would base on method completion time. But dunno.
You can not use UIProgressView as its difficult to know how much time it will take to perform image operations.
you can try this MBProgressHUD
Put this code in didFinishPickingMediaWithInfo
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = #"Saving Image...";
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:#selector(savingPicture:) onTarget:self withObject:selectedImage animated:YES];
and put your resize/crop image operations code in savingPicture method.
Progress views are more appropriate when you are running a loop and you know what percentage of the work is being completed, to show the progress in the progress bar. In this case you can show a UIActivityIndicator instead, until your image is ready to be put on the image view.
I want to state my basic requirement which is to change the frame for crop rect in UIImagePickerController for a camera.
I just realized that it is not possible to change frame for crop rect. That leaves me with only one option i.e to create my own camera overlay wherein I can set frame for crop rect. I searched a lot but found nothing. I asked previously but didn't get anything. I don't even know is it possible and if yes then how to create it and move the crop box, scale it in accordance to default UIImagePickerController crop rect.
You have to Implement your own CropRect. First set the
[picker setAllowsEditing:NO];
Then in didFinishPickingMediaWithInfo delegate Push your own CropRect View
CustomImageEditor *custom = [[CustomImageEditor alloc] initWithNibName:#"CustomImageEditor" bundle:nil];
[picker pushViewController:custom animated:YES];
[custom release];
while Pushing view pass the image to the Custom View like this
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
custom.pickedImage = image;
In that customView you crop the image.
For croping the image try like this..
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *image = [UIImage imageWithCGImage:imageRef];
I am developing an Application where I am capturing image in portrait mode and then moving to another class with Image data and showing that image in a UIImageView on a UIScrollView.
Now If I am taking image form library,it is working fine, but If I have to capture an Image it is showing data in landscape mode. Below is my code of both classes. :
Class 1:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *imgData = UIImagePNGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"]);
[picker dismissModalViewControllerAnimated:YES];
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop];
PhotoCropperPage *photoCropper = [[PhotoCropperPage alloc]
initWithPhoto:photo
delegate:self
uiMode:PCPUIModePresentedAsModalViewController
showsInfoButton:YES];
[photoCropper setMinZoomScale:0.3f];
[photoCropper setMaxZoomScale:4.0f];
[self.navigationController pushViewController:photoCropper animated:YES];
}
Class 2:
- (void) loadPhoto
{
if (self.photo == nil) {
return;
}
CGFloat w = self.photo.size.width;
CGFloat h = self.photo.size.height;
CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
self.scrollView.contentSize = imageViewFrame.size;
UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
iv.image = self.photo;
[self.scrollView addSubview:iv];
self.imageView = iv;
[iv release];
}
I am not getting where I am doing a mistake? Can anybody point me out? Thanks in advance.
Just found an awesome answer to your question. Here's the link: https://stackoverflow.com/a/5427890/1047258
And then you can do something like that:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *theImage = [[info objectForKey:#"UIImagePickerControllerOriginalImage"]fixOrientation];
//rest of your code
}
and theImage will be your image with correct orientation. It worked in my project
Hope it helps.
be careful not to throw away the orientation data... Try this:
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop scale:image.scale orientation:image.orientation ];
also, you'll want to make sure you're cropping the right pixels depending in the image's orientation.
I have this iPhone application that let users take a picture and save it in a database online. My problem is that every time an user take a picture and saves it, the picture results to be in landscape, even though it was taken in portrait mode. This results in having the portrait picture stretched.
This is the code I use when taking a picture:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
NSData *imgData=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
UIImage *img=[[UIImage alloc] initWithData:imgData];
if(img.size.width < img.size.height){
NSLog(#"width < height");
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}
else{
NSLog(#"width > height");
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 460, 320)];
}
imageView.image = img;
[img release];
[[self view] fillPreviewWithImg: imageView];
[[self view] setImage: imageView.image];
}
Basically what I do is take the picture, create a UIImage, check if it's portrait or landscape, create the corresponding UIImageView and then set the image into the UIImageView. After that I just call a couple of methods to set up the image in the main view.
I believe the problem with stretching is not bounded to the PHP but to the Objective-C code, but I can't really see how or why this behavior happens.
Does anyone of you have an idea?
Thanks,
Masiar
Have a look at the imageOrientation property of the UIImage class. You can have an image that is 320 wide and 480 high, but with an orientation of Landscape. This is contained in the EXIF information and it is up to the viewing program to use this orientation information to rotate the image appropriately. Just checking the width and height of the image is not sufficient to know the orientation and this is causing your stretching that you are seeing.