Skip to content

Instantly share code, notes, and snippets.

@vikage
Last active February 27, 2017 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikage/8eb1ce37a899b6537ae1767d189a27ec to your computer and use it in GitHub Desktop.
Save vikage/8eb1ce37a899b6537ae1767d189a27ec to your computer and use it in GitHub Desktop.
Fix UIImagePickerController allowEditing bug
@implementation UpdateAvatarViewController
{
UIScrollView *cropImageScrollView;
}
-(void) chooseCameraPicture
{
cropImageScrollView = nil;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{
[self fixImagePicker:picker];
}];
}
-(void) fixImagePicker:(UIImagePickerController *)imagePicker
{
if (imagePicker.sourceType != UIImagePickerControllerSourceTypeCamera || imagePicker.allowsEditing == NO)
{
return;
}
if (cropImageScrollView)
{
CGSize size = cropImageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
cropImageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
CGSize contentSize = cropImageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
cropImageScrollView.contentOffset = CGPointMake(cropImageScrollView.contentOffset.x, offset);
}
return;
}
Class CropViewClass = [NSClassFromString(@"PLCropOverlayCropView") class];
for (UIView *subview in [imagePicker.view allSubViews])
{
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
if ([subview isKindOfClass:[NSClassFromString(@"PLImageScrollView") class]])
{
cropImageScrollView = (UIScrollView *)subview;
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self fixImagePicker:imagePicker];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment