问题
I am working on camera app. I am capturing an image by using UIImagePickerController.After that before saving it in gallery I want to crop the image and need to save the cropped image in gallery.
I searched on google ,I found samples to pick the images from gallery and then cropping which is not my requirement.Could any body have worked on this??? If you have gone through any tutorials/sample code please post the links.Thanks in advance
回答1:
pust isallowediting=yes; for picker so that we can edit image which you captured and selected from gallery also
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.isallowediting = yes;
hope these will help you
回答2:
Use this code in imagePickerController method to crop ur captured image. its working fine for me.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[_imagePickerControllerInstance dismissModalViewControllerAnimated:YES];
UIImage *_pickedAvatar = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *croppedImage = [self cropImage:_pickedAvatar andFrame:CGRectMake(0, 0, 250, 250)];
// croppedImage : U can use this cropped image for saving in gallery.
}
- (UIImage *)cropImage:(UIImage*)image andFrame:(CGRect)rect {
//Note : rec is nothing but the image frame which u want to crop exactly.
rect = CGRectMake(rect.origin.x*image.scale,
rect.origin.y*image.scale,
rect.size.width*image.scale,
rect.size.height*image.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:image.scale
orientation:image.imageOrientation];
CGImageRelease(imageRef);
return result;
}
else
use this link
Cropping an UIImage
Use UIImage category and use wherever u need to crop image.
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect {
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
来源:https://stackoverflow.com/questions/15288092/how-to-crop-the-captured-image-before-saving-it-in-gallery-in-ios