UIImagePicker allowsEditing stuck in center
I have a UIImagePicker that works perfect for a type of UIImagePickerControllerSourceTypePhotoLibrary, but when I use UIImagePickerControllerSourceTypeCamera, the editing box cannot move from the center of the image. So if the image is say taller than it is wide, the user cannot move the editing box to the top square of the image.
Anyone know why this would be the case? It only happens when the source is from the camera, not the library.
Edit: Some CODE!!!
if (actionSheet.tag == 2) {
if (buttonIndex == 0) { // Camera
// Check for camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
else if (buttonIndex == 1) { // Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
So as you can see, I display them the exact same, but the camera edit acts differently than the photo library edit.
iphone ios uiimagepickercontroller
add a comment |
I have a UIImagePicker that works perfect for a type of UIImagePickerControllerSourceTypePhotoLibrary, but when I use UIImagePickerControllerSourceTypeCamera, the editing box cannot move from the center of the image. So if the image is say taller than it is wide, the user cannot move the editing box to the top square of the image.
Anyone know why this would be the case? It only happens when the source is from the camera, not the library.
Edit: Some CODE!!!
if (actionSheet.tag == 2) {
if (buttonIndex == 0) { // Camera
// Check for camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
else if (buttonIndex == 1) { // Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
So as you can see, I display them the exact same, but the camera edit acts differently than the photo library edit.
iphone ios uiimagepickercontroller
add a comment |
I have a UIImagePicker that works perfect for a type of UIImagePickerControllerSourceTypePhotoLibrary, but when I use UIImagePickerControllerSourceTypeCamera, the editing box cannot move from the center of the image. So if the image is say taller than it is wide, the user cannot move the editing box to the top square of the image.
Anyone know why this would be the case? It only happens when the source is from the camera, not the library.
Edit: Some CODE!!!
if (actionSheet.tag == 2) {
if (buttonIndex == 0) { // Camera
// Check for camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
else if (buttonIndex == 1) { // Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
So as you can see, I display them the exact same, but the camera edit acts differently than the photo library edit.
iphone ios uiimagepickercontroller
I have a UIImagePicker that works perfect for a type of UIImagePickerControllerSourceTypePhotoLibrary, but when I use UIImagePickerControllerSourceTypeCamera, the editing box cannot move from the center of the image. So if the image is say taller than it is wide, the user cannot move the editing box to the top square of the image.
Anyone know why this would be the case? It only happens when the source is from the camera, not the library.
Edit: Some CODE!!!
if (actionSheet.tag == 2) {
if (buttonIndex == 0) { // Camera
// Check for camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
else if (buttonIndex == 1) { // Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentViewController:imagePicker
animated:YES
completion:^(void) {
}];
}
}
So as you can see, I display them the exact same, but the camera edit acts differently than the photo library edit.
iphone ios uiimagepickercontroller
iphone ios uiimagepickercontroller
edited Sep 28 '12 at 18:29
Eric
asked Sep 27 '12 at 21:16
EricEric
4,54132441
4,54132441
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Looks like this behavior is just a bug in iOS 6... Basically you cannot move the editing box, it always bounces back to the middle unless you zoom in a bit. Hopefully they fix that soon.
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
|
show 21 more comments
This is the default behavior the Image Picker Controller, you can not change it. The only other option is to create your own cropping utility. Check out the link below for an example:
https://github.com/ardalahmet/SSPhotoCropperViewController
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
|
show 2 more comments
I know, this is not a good solution, but it works.
I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.
CAUTION: PLImageScrollView
and PLCropOverlayCropView
are UNDOCUMENTED classes.
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
[self fxxxImagePickerController:imagePickerController];
}];
}
- (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
if (!imagePickerController
|| !imagePickerController.allowsEditing
|| imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
return;
}
// !!!: UNDOCUMENTED CLASS
Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
[imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
else if ([subview isKindOfClass:[UIScrollView class]]
&& [subview isKindOfClass:ScrollViewClass]) {
BOOL isNewImageScrollView = !self->_imageScrollView;
self->_imageScrollView = (UIScrollView *)subview;
// 1. enable scrolling
CGSize size = self->_imageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
// 2. centering image by default
if (isNewImageScrollView) {
CGSize contentSize = self->_imageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
}
}
}
return YES;
}];
// prevent re-layout, maybe not necessary
@weakify(self, imagePickerController);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self, imagePickerController);
[self fxxxImagePickerController:imagePickerController];
});
}
EDIT: The eachSubview:
method traverses all the subviews tree.
add a comment |
If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using
UIApplication.shared.statusBarStyle = .lightContent
or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)
Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
var pickedImage : UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage
{
pickedImage = img
}
else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
{
pickedImage = img
}
dismiss(animated: true, completion: {
UIApplication.shared.statusBarStyle = .lightContent
})}
Apparently this is a workaround for the same.Hope this helps.
add a comment |
Stupid answer
Reset contentInset of scrollview
#import <objc/runtime.h>
@interface WeakObjectContainer : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainer
- (instancetype) initWithObject:(id)object
{
if (!(self = [super init]))
return nil;
_object = object;
return self;
}
@end
@implementation UIImagePickerController (PLUS)
// MARK: - Create a weak stored property in extension
- (id)weakObjectForKey:(const void*)key {
WeakObjectContainer *container = objc_getAssociatedObject(self, key);
return [container object];
}
- (void)setWeakObject:(id)object forKey:(const void*)key {
WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object];
objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// MARK: - Create a weak property for scrollView
- (UIScrollView *)scrollView {
return [self weakObjectForKey:_cmd];
}
- (void)setScrollView:(UIScrollView *)scrollView {
[self setWeakObject:scrollView forKey:@selector(scrollView)];
}
// MARK: - Create a weak property for cropView
- (UIView *)cropView {
return [self weakObjectForKey:_cmd];
}
- (void)setCropView:(UIView *)cropView {
[self setWeakObject:cropView forKey:@selector(cropView)];
}
// MARK: - Fix cannot move editing xox
- (void)fixCannotMoveEditingBox {
if (!self.scrollView || !self.cropView) {
UIView *view = [self view];
self.scrollView = [self findScrollView:view];
self.cropView = [self findCropView:view];
if (self.scrollView && self.cropView) {
CGFloat top = self.cropView.frame.origin.y;
CGFloat bottom = ({
self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y;
});
self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
self.scrollView.contentOffset = CGPointMake(0, -1);
}
}
__weak typeof(self) weakself = self;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
__strong typeof(weakself) strongself = weakself;
[strongself fixCannotMoveEditingBox];
});
}
- (UIScrollView *)findScrollView:(UIView *)view {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView *)view;
}
for (UIView *subview in [view subviews]) {
UIScrollView *view = [self findScrollView:subview];
if (view) {
return view;
}
}
return nil;
}
- (UIView *)findCropView:(UIView *)view {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGSize size = [view frame].size;
if (size.width == width && size.height == width) {
return view;
}
for (UIView *subview in [view subviews]) {
UIView *view = [self findCropView:subview];
if (view) {
return view;
}
}
return nil;
}
@end
then call it
[imagePickerController fixCannotMoveEditingBox];
add a comment |
A workaround that solved it is to add an entry in info.plist with "View controller-based status bar appearance" set to NO
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12630155%2fuiimagepicker-allowsediting-stuck-in-center%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like this behavior is just a bug in iOS 6... Basically you cannot move the editing box, it always bounces back to the middle unless you zoom in a bit. Hopefully they fix that soon.
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
|
show 21 more comments
Looks like this behavior is just a bug in iOS 6... Basically you cannot move the editing box, it always bounces back to the middle unless you zoom in a bit. Hopefully they fix that soon.
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
|
show 21 more comments
Looks like this behavior is just a bug in iOS 6... Basically you cannot move the editing box, it always bounces back to the middle unless you zoom in a bit. Hopefully they fix that soon.
Looks like this behavior is just a bug in iOS 6... Basically you cannot move the editing box, it always bounces back to the middle unless you zoom in a bit. Hopefully they fix that soon.
answered Oct 31 '12 at 20:41
EricEric
4,54132441
4,54132441
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
|
show 21 more comments
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
6
6
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
I think this is still a bug in iOS 7
– race_carr
Jul 18 '14 at 16:17
9
9
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
aaaand still a bug in iOS 8. :(
– Alex Ryan
Aug 12 '14 at 22:36
9
9
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
Same bug exists in even iOS 8.1
– Adnan
Nov 4 '14 at 11:22
5
5
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
still strong in 8.2
– Jkmn
Mar 24 '15 at 17:19
88
88
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
I'm from the future, still a bug in iOS 12.0
– Brent Dillingham
Mar 28 '15 at 4:46
|
show 21 more comments
This is the default behavior the Image Picker Controller, you can not change it. The only other option is to create your own cropping utility. Check out the link below for an example:
https://github.com/ardalahmet/SSPhotoCropperViewController
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
|
show 2 more comments
This is the default behavior the Image Picker Controller, you can not change it. The only other option is to create your own cropping utility. Check out the link below for an example:
https://github.com/ardalahmet/SSPhotoCropperViewController
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
|
show 2 more comments
This is the default behavior the Image Picker Controller, you can not change it. The only other option is to create your own cropping utility. Check out the link below for an example:
https://github.com/ardalahmet/SSPhotoCropperViewController
This is the default behavior the Image Picker Controller, you can not change it. The only other option is to create your own cropping utility. Check out the link below for an example:
https://github.com/ardalahmet/SSPhotoCropperViewController
answered Sep 27 '12 at 22:08
VikingsVikings
1,8122845
1,8122845
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
|
show 2 more comments
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
2
2
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
But it works when you pick from an album.. Doesn't make sense. Why would you be able to move it more when its from an album rather than camera
– Eric
Sep 28 '12 at 4:24
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
@Eric you might have to post some code
– Vikings
Sep 28 '12 at 17:45
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
There is the code I use to display it
– Eric
Sep 28 '12 at 18:30
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
@Eric the editing box does not move at all, it is fixed in the middle of the screen, and you are able to scale the picture to fit in the box. Take a screenshot, because your code is right
– Vikings
Sep 28 '12 at 21:45
1
1
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
this isnt the default behaviour, i believe it is a bug in ios6. On previous ios versions it works fine, but on ios6 the image bounces back when you try and move it. However it does work slightly if you zoom in a bit.
– Robert
Oct 24 '12 at 22:44
|
show 2 more comments
I know, this is not a good solution, but it works.
I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.
CAUTION: PLImageScrollView
and PLCropOverlayCropView
are UNDOCUMENTED classes.
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
[self fxxxImagePickerController:imagePickerController];
}];
}
- (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
if (!imagePickerController
|| !imagePickerController.allowsEditing
|| imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
return;
}
// !!!: UNDOCUMENTED CLASS
Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
[imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
else if ([subview isKindOfClass:[UIScrollView class]]
&& [subview isKindOfClass:ScrollViewClass]) {
BOOL isNewImageScrollView = !self->_imageScrollView;
self->_imageScrollView = (UIScrollView *)subview;
// 1. enable scrolling
CGSize size = self->_imageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
// 2. centering image by default
if (isNewImageScrollView) {
CGSize contentSize = self->_imageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
}
}
}
return YES;
}];
// prevent re-layout, maybe not necessary
@weakify(self, imagePickerController);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self, imagePickerController);
[self fxxxImagePickerController:imagePickerController];
});
}
EDIT: The eachSubview:
method traverses all the subviews tree.
add a comment |
I know, this is not a good solution, but it works.
I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.
CAUTION: PLImageScrollView
and PLCropOverlayCropView
are UNDOCUMENTED classes.
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
[self fxxxImagePickerController:imagePickerController];
}];
}
- (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
if (!imagePickerController
|| !imagePickerController.allowsEditing
|| imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
return;
}
// !!!: UNDOCUMENTED CLASS
Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
[imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
else if ([subview isKindOfClass:[UIScrollView class]]
&& [subview isKindOfClass:ScrollViewClass]) {
BOOL isNewImageScrollView = !self->_imageScrollView;
self->_imageScrollView = (UIScrollView *)subview;
// 1. enable scrolling
CGSize size = self->_imageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
// 2. centering image by default
if (isNewImageScrollView) {
CGSize contentSize = self->_imageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
}
}
}
return YES;
}];
// prevent re-layout, maybe not necessary
@weakify(self, imagePickerController);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self, imagePickerController);
[self fxxxImagePickerController:imagePickerController];
});
}
EDIT: The eachSubview:
method traverses all the subviews tree.
add a comment |
I know, this is not a good solution, but it works.
I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.
CAUTION: PLImageScrollView
and PLCropOverlayCropView
are UNDOCUMENTED classes.
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
[self fxxxImagePickerController:imagePickerController];
}];
}
- (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
if (!imagePickerController
|| !imagePickerController.allowsEditing
|| imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
return;
}
// !!!: UNDOCUMENTED CLASS
Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
[imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
else if ([subview isKindOfClass:[UIScrollView class]]
&& [subview isKindOfClass:ScrollViewClass]) {
BOOL isNewImageScrollView = !self->_imageScrollView;
self->_imageScrollView = (UIScrollView *)subview;
// 1. enable scrolling
CGSize size = self->_imageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
// 2. centering image by default
if (isNewImageScrollView) {
CGSize contentSize = self->_imageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
}
}
}
return YES;
}];
// prevent re-layout, maybe not necessary
@weakify(self, imagePickerController);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self, imagePickerController);
[self fxxxImagePickerController:imagePickerController];
});
}
EDIT: The eachSubview:
method traverses all the subviews tree.
I know, this is not a good solution, but it works.
I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.
CAUTION: PLImageScrollView
and PLCropOverlayCropView
are UNDOCUMENTED classes.
- (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [UIImagePickerController new];
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
[self fxxxImagePickerController:imagePickerController];
}];
}
- (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
if (!imagePickerController
|| !imagePickerController.allowsEditing
|| imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
return;
}
// !!!: UNDOCUMENTED CLASS
Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
[imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
if ([subview isKindOfClass:CropViewClass]) {
// 0. crop rect position
subview.frame = subview.superview.bounds;
}
else if ([subview isKindOfClass:[UIScrollView class]]
&& [subview isKindOfClass:ScrollViewClass]) {
BOOL isNewImageScrollView = !self->_imageScrollView;
self->_imageScrollView = (UIScrollView *)subview;
// 1. enable scrolling
CGSize size = self->_imageScrollView.frame.size;
CGFloat inset = ABS(size.width - size.height) / 2;
self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
// 2. centering image by default
if (isNewImageScrollView) {
CGSize contentSize = self->_imageScrollView.contentSize;
if (contentSize.height > contentSize.width) {
CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
}
}
}
return YES;
}];
// prevent re-layout, maybe not necessary
@weakify(self, imagePickerController);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@strongify(self, imagePickerController);
[self fxxxImagePickerController:imagePickerController];
});
}
EDIT: The eachSubview:
method traverses all the subviews tree.
answered Nov 1 '16 at 6:57
iwilliwill
1,41942141
1,41942141
add a comment |
add a comment |
If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using
UIApplication.shared.statusBarStyle = .lightContent
or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)
Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
var pickedImage : UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage
{
pickedImage = img
}
else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
{
pickedImage = img
}
dismiss(animated: true, completion: {
UIApplication.shared.statusBarStyle = .lightContent
})}
Apparently this is a workaround for the same.Hope this helps.
add a comment |
If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using
UIApplication.shared.statusBarStyle = .lightContent
or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)
Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
var pickedImage : UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage
{
pickedImage = img
}
else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
{
pickedImage = img
}
dismiss(animated: true, completion: {
UIApplication.shared.statusBarStyle = .lightContent
})}
Apparently this is a workaround for the same.Hope this helps.
add a comment |
If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using
UIApplication.shared.statusBarStyle = .lightContent
or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)
Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
var pickedImage : UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage
{
pickedImage = img
}
else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
{
pickedImage = img
}
dismiss(animated: true, completion: {
UIApplication.shared.statusBarStyle = .lightContent
})}
Apparently this is a workaround for the same.Hope this helps.
If you have set "View controller-based status bar appearance" to NO in info.plist and set status bar appearance as light using
UIApplication.shared.statusBarStyle = .lightContent
or using any other method , Then simply set the style as .default before presenting the image picker. for Eg:
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
UIApplication.shared.statusBarStyle = .default
present(imagePicker, animated: true, completion: nil)
Change the source type according to your need either as photoLibrary or camera and in completion block of your didFinishPickingMediaWithInfo add the following to completion block.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
var pickedImage : UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage
{
pickedImage = img
}
else if let img = info[UIImagePickerControllerOriginalImage] as? UIImage
{
pickedImage = img
}
dismiss(animated: true, completion: {
UIApplication.shared.statusBarStyle = .lightContent
})}
Apparently this is a workaround for the same.Hope this helps.
answered Aug 20 '18 at 8:13
ashin asokashin asok
78211
78211
add a comment |
add a comment |
Stupid answer
Reset contentInset of scrollview
#import <objc/runtime.h>
@interface WeakObjectContainer : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainer
- (instancetype) initWithObject:(id)object
{
if (!(self = [super init]))
return nil;
_object = object;
return self;
}
@end
@implementation UIImagePickerController (PLUS)
// MARK: - Create a weak stored property in extension
- (id)weakObjectForKey:(const void*)key {
WeakObjectContainer *container = objc_getAssociatedObject(self, key);
return [container object];
}
- (void)setWeakObject:(id)object forKey:(const void*)key {
WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object];
objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// MARK: - Create a weak property for scrollView
- (UIScrollView *)scrollView {
return [self weakObjectForKey:_cmd];
}
- (void)setScrollView:(UIScrollView *)scrollView {
[self setWeakObject:scrollView forKey:@selector(scrollView)];
}
// MARK: - Create a weak property for cropView
- (UIView *)cropView {
return [self weakObjectForKey:_cmd];
}
- (void)setCropView:(UIView *)cropView {
[self setWeakObject:cropView forKey:@selector(cropView)];
}
// MARK: - Fix cannot move editing xox
- (void)fixCannotMoveEditingBox {
if (!self.scrollView || !self.cropView) {
UIView *view = [self view];
self.scrollView = [self findScrollView:view];
self.cropView = [self findCropView:view];
if (self.scrollView && self.cropView) {
CGFloat top = self.cropView.frame.origin.y;
CGFloat bottom = ({
self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y;
});
self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
self.scrollView.contentOffset = CGPointMake(0, -1);
}
}
__weak typeof(self) weakself = self;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
__strong typeof(weakself) strongself = weakself;
[strongself fixCannotMoveEditingBox];
});
}
- (UIScrollView *)findScrollView:(UIView *)view {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView *)view;
}
for (UIView *subview in [view subviews]) {
UIScrollView *view = [self findScrollView:subview];
if (view) {
return view;
}
}
return nil;
}
- (UIView *)findCropView:(UIView *)view {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGSize size = [view frame].size;
if (size.width == width && size.height == width) {
return view;
}
for (UIView *subview in [view subviews]) {
UIView *view = [self findCropView:subview];
if (view) {
return view;
}
}
return nil;
}
@end
then call it
[imagePickerController fixCannotMoveEditingBox];
add a comment |
Stupid answer
Reset contentInset of scrollview
#import <objc/runtime.h>
@interface WeakObjectContainer : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainer
- (instancetype) initWithObject:(id)object
{
if (!(self = [super init]))
return nil;
_object = object;
return self;
}
@end
@implementation UIImagePickerController (PLUS)
// MARK: - Create a weak stored property in extension
- (id)weakObjectForKey:(const void*)key {
WeakObjectContainer *container = objc_getAssociatedObject(self, key);
return [container object];
}
- (void)setWeakObject:(id)object forKey:(const void*)key {
WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object];
objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// MARK: - Create a weak property for scrollView
- (UIScrollView *)scrollView {
return [self weakObjectForKey:_cmd];
}
- (void)setScrollView:(UIScrollView *)scrollView {
[self setWeakObject:scrollView forKey:@selector(scrollView)];
}
// MARK: - Create a weak property for cropView
- (UIView *)cropView {
return [self weakObjectForKey:_cmd];
}
- (void)setCropView:(UIView *)cropView {
[self setWeakObject:cropView forKey:@selector(cropView)];
}
// MARK: - Fix cannot move editing xox
- (void)fixCannotMoveEditingBox {
if (!self.scrollView || !self.cropView) {
UIView *view = [self view];
self.scrollView = [self findScrollView:view];
self.cropView = [self findCropView:view];
if (self.scrollView && self.cropView) {
CGFloat top = self.cropView.frame.origin.y;
CGFloat bottom = ({
self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y;
});
self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
self.scrollView.contentOffset = CGPointMake(0, -1);
}
}
__weak typeof(self) weakself = self;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
__strong typeof(weakself) strongself = weakself;
[strongself fixCannotMoveEditingBox];
});
}
- (UIScrollView *)findScrollView:(UIView *)view {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView *)view;
}
for (UIView *subview in [view subviews]) {
UIScrollView *view = [self findScrollView:subview];
if (view) {
return view;
}
}
return nil;
}
- (UIView *)findCropView:(UIView *)view {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGSize size = [view frame].size;
if (size.width == width && size.height == width) {
return view;
}
for (UIView *subview in [view subviews]) {
UIView *view = [self findCropView:subview];
if (view) {
return view;
}
}
return nil;
}
@end
then call it
[imagePickerController fixCannotMoveEditingBox];
add a comment |
Stupid answer
Reset contentInset of scrollview
#import <objc/runtime.h>
@interface WeakObjectContainer : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainer
- (instancetype) initWithObject:(id)object
{
if (!(self = [super init]))
return nil;
_object = object;
return self;
}
@end
@implementation UIImagePickerController (PLUS)
// MARK: - Create a weak stored property in extension
- (id)weakObjectForKey:(const void*)key {
WeakObjectContainer *container = objc_getAssociatedObject(self, key);
return [container object];
}
- (void)setWeakObject:(id)object forKey:(const void*)key {
WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object];
objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// MARK: - Create a weak property for scrollView
- (UIScrollView *)scrollView {
return [self weakObjectForKey:_cmd];
}
- (void)setScrollView:(UIScrollView *)scrollView {
[self setWeakObject:scrollView forKey:@selector(scrollView)];
}
// MARK: - Create a weak property for cropView
- (UIView *)cropView {
return [self weakObjectForKey:_cmd];
}
- (void)setCropView:(UIView *)cropView {
[self setWeakObject:cropView forKey:@selector(cropView)];
}
// MARK: - Fix cannot move editing xox
- (void)fixCannotMoveEditingBox {
if (!self.scrollView || !self.cropView) {
UIView *view = [self view];
self.scrollView = [self findScrollView:view];
self.cropView = [self findCropView:view];
if (self.scrollView && self.cropView) {
CGFloat top = self.cropView.frame.origin.y;
CGFloat bottom = ({
self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y;
});
self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
self.scrollView.contentOffset = CGPointMake(0, -1);
}
}
__weak typeof(self) weakself = self;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
__strong typeof(weakself) strongself = weakself;
[strongself fixCannotMoveEditingBox];
});
}
- (UIScrollView *)findScrollView:(UIView *)view {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView *)view;
}
for (UIView *subview in [view subviews]) {
UIScrollView *view = [self findScrollView:subview];
if (view) {
return view;
}
}
return nil;
}
- (UIView *)findCropView:(UIView *)view {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGSize size = [view frame].size;
if (size.width == width && size.height == width) {
return view;
}
for (UIView *subview in [view subviews]) {
UIView *view = [self findCropView:subview];
if (view) {
return view;
}
}
return nil;
}
@end
then call it
[imagePickerController fixCannotMoveEditingBox];
Stupid answer
Reset contentInset of scrollview
#import <objc/runtime.h>
@interface WeakObjectContainer : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainer
- (instancetype) initWithObject:(id)object
{
if (!(self = [super init]))
return nil;
_object = object;
return self;
}
@end
@implementation UIImagePickerController (PLUS)
// MARK: - Create a weak stored property in extension
- (id)weakObjectForKey:(const void*)key {
WeakObjectContainer *container = objc_getAssociatedObject(self, key);
return [container object];
}
- (void)setWeakObject:(id)object forKey:(const void*)key {
WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object];
objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// MARK: - Create a weak property for scrollView
- (UIScrollView *)scrollView {
return [self weakObjectForKey:_cmd];
}
- (void)setScrollView:(UIScrollView *)scrollView {
[self setWeakObject:scrollView forKey:@selector(scrollView)];
}
// MARK: - Create a weak property for cropView
- (UIView *)cropView {
return [self weakObjectForKey:_cmd];
}
- (void)setCropView:(UIView *)cropView {
[self setWeakObject:cropView forKey:@selector(cropView)];
}
// MARK: - Fix cannot move editing xox
- (void)fixCannotMoveEditingBox {
if (!self.scrollView || !self.cropView) {
UIView *view = [self view];
self.scrollView = [self findScrollView:view];
self.cropView = [self findCropView:view];
if (self.scrollView && self.cropView) {
CGFloat top = self.cropView.frame.origin.y;
CGFloat bottom = ({
self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y;
});
self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
self.scrollView.contentOffset = CGPointMake(0, -1);
}
}
__weak typeof(self) weakself = self;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
__strong typeof(weakself) strongself = weakself;
[strongself fixCannotMoveEditingBox];
});
}
- (UIScrollView *)findScrollView:(UIView *)view {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView *)view;
}
for (UIView *subview in [view subviews]) {
UIScrollView *view = [self findScrollView:subview];
if (view) {
return view;
}
}
return nil;
}
- (UIView *)findCropView:(UIView *)view {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGSize size = [view frame].size;
if (size.width == width && size.height == width) {
return view;
}
for (UIView *subview in [view subviews]) {
UIView *view = [self findCropView:subview];
if (view) {
return view;
}
}
return nil;
}
@end
then call it
[imagePickerController fixCannotMoveEditingBox];
answered Nov 23 '18 at 3:16
yyckingyycking
22128
22128
add a comment |
add a comment |
A workaround that solved it is to add an entry in info.plist with "View controller-based status bar appearance" set to NO
add a comment |
A workaround that solved it is to add an entry in info.plist with "View controller-based status bar appearance" set to NO
add a comment |
A workaround that solved it is to add an entry in info.plist with "View controller-based status bar appearance" set to NO
A workaround that solved it is to add an entry in info.plist with "View controller-based status bar appearance" set to NO
answered Apr 18 '17 at 14:27
Josep AlsinaJosep Alsina
1,10679
1,10679
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12630155%2fuiimagepicker-allowsediting-stuck-in-center%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown