UIImage 不会裁剪
UIImage won't crop
用户使用相机拍照(UIImagePickerController
)。但是,我无法裁剪它 - 或者我尝试了它 returns 完全相同的照片。
在 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];
returns 无
It's different from [this post][2] because, as the bounty answer points out,
"The AllowsEditing property simply allows the user to crop to a square if picking an image." 用户没有选择图像,我也没有尝试专门裁剪到正方形。我正在尝试裁剪它。
你 return 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);
拿一个 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];
}
用户使用相机拍照(UIImagePickerController
)。但是,我无法裁剪它 - 或者我尝试了它 returns 完全相同的照片。
在 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];
returns 无
It's different from [this post][2] because, as the bounty answer points out,
"The AllowsEditing property simply allows the user to crop to a square if picking an image." 用户没有选择图像,我也没有尝试专门裁剪到正方形。我正在尝试裁剪它。
你 return 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);
拿一个 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];
}