用户使用相机拍摄照片(UIImagePickerController
)。但是,我无法裁剪它 - 或者我尝试,它会返回完全相同的照片。
在viewDidAppear
我创建了图像选择器控制器:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"SnapItCameraView" owner:self options:nil];
self.overlayView.frame = ipc.cameraOverlayView.frame;
ipc.cameraOverlayView = self.overlayView;
self.overlayView = nil;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0);
ipc.cameraViewTransform = translate;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
CGFloat scaleCamera = screenHeight / 426;
CGAffineTransform scale = CGAffineTransformScale(translate, scaleCamera, scaleCamera);
ipc.cameraViewTransform = scale;
ipc.showsCameraControls = NO;
ipc.tabBarController.tabBar.hidden = YES;
ipc.allowsEditing = YES;
[ipc setAllowsEditing:YES];
[self presentViewController:ipc animated:YES completion:nil];
}
在didFinishPickingMediaWithInfo
UIImage* theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(theImage, nil, nil, nil);
[self ordinaryCrop:theImage toRect:CGRectMake(20, 0, 250, 250)];
UIImageWriteToSavedPhotosAlbum(theImage, nil, nil, nil);
剪切:
-(UIImage *)ordinaryCrop:(UIImage *)imageToCrop toRect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], cropRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
但裁剪后图像完全相同。我尝试了解决方案in this post,但是
UIImage* theImage = [info objectForKey:UIImagePickerControllerEditedImage];
返回nil
It's different from [this post][2] because, as the bounty answer points out,
"如果选择图像,则AllowEditing属性只允许用户裁剪为正方形。"用户没有选择图像,我并没有特别尝试裁剪到正方形。我试图完全裁剪它。
答案 0 :(得分:0)
您从cropped
功能返回ordinaryCrop
,但在didFinishPickingMediaWithInfo
中没有对其执行任何操作,只需再次保存原始图像。
你想要的是:
UIImage* theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(theImage, nil, nil, nil);
UIImage *croppedImage = [self ordinaryCrop:theImage toRect:CGRectMake(20, 0, 250, 250)];
UIImageWriteToSavedPhotosAlbum(croppedImage, nil, nil, nil);
答案 1 :(得分:0)
选择UIButton并尝试:
<强> 1 强>
UIButton *addImage_Button;
<强> 2 强>
-(IBAction)addImage_Button:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES; //THIS IS FOR CROPPING
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
3。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenUserImage = info[UIImagePickerControllerEditedImage];
chosenUserImage = [chosenUserImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
addImage_Button.imageView.contentMode = UIViewContentModeScaleAspectFill;
[addImage_Button setImage:chosenUserImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
<强> 4 强>
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}