执行代码 /after/ UIImagePicker 被解雇
我有一个简单的 iPhone 应用程序。
我显示一个 UIImagePicker,并让用户选择一个图像。
然后,我在图像上执行一些代码,并希望此代码在 UIImagePicker 被关闭之后执行。
编辑:更新了代码,问题仍然存在:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData* data = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);
[[picker parentViewController] dismissModalViewControllerAnimated:TRUE];
[self executeSomeCode: data];
}
但是,当我运行代码时,它挂在 UIImagePicker 上以运行我的代码,然后在执行所述代码后 UIImagePicker 在视觉上被忽略。
有什么想法吗?
I have a simple iPhone application.
I display a UIImagePicker, and let the user select an image.
I then execute some code on the image, and want this code to execute after the UIImagePicker has been dismissed.
EDIT: updated code, problem remains:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData* data = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);
[[picker parentViewController] dismissModalViewControllerAnimated:TRUE];
[self executeSomeCode: data];
}
However, when I run the code, it hangs on the UIImagePicker to run my code, and then the UIImagePicker is visually dismissed after said code has executed.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我意识到这不是最优雅的答案。我以前也遇到过类似的问题(尝试在 viewDidAppear 中执行视觉操作但不起作用)。我发现通过调用
performSelector:withObject:afterDelay:
导致我看到了视觉效果。所以在你的情况下,它会是:
我不知道这是否适合你(或者为什么它对我有用)。
I realize this isn't the most graceful of answers. I've had similar problems before (trying to do visual things in viewDidAppear that wasn't working). I found that by calling
performSelector:withObject:afterDelay:
caused my visual effects to be seen.So in your case it would be:
I have no idea if this will work for you (or why it worked for me).
不必使用后台方法,但您可以尝试以下方法:
It shouldn't be necessary to use a background method, but you can try this approach:
http://developer.apple.com/ iphone/library/documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html
我相信您的调用在 iOS3 中已被弃用
您可以尝试
在关闭 viewController 之前从图像中获取数据
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html
I believe your call was deprecated in iOS3
You might try
and the get the data from the image before dismissing the viewController
我不认为你挂在你认为的地方。据我所知, UIImagePNGRepresentation 需要相当长的时间才能执行(如果您不介意不无损的话,UIImageJPEGRepresentation 要快得多)。您可以将字典传递到您的方法中并在其中获取您需要的 NSData。
你的第二个问题是你的代码是同步的,无论你多久告诉你的控制器关闭,它都会等待你的长代码运行。要在下一次运行循环迭代中安排繁重的代码并让控制器关闭,请使用 0ms 延迟来执行它。
我会将你的代码更改为:
I don't think you are hanging where you think you are. UIImagePNGRepresentation takes quite some time to perform as far as I know (UIImageJPEGRepresentation is much faster, if you don't mind not being lossless). You could pass the dictionary into your method and obtain the NSData you need in there.
Your second problem is that your code is synchronous, no matter how soon you tell your controller to dismiss, it will wait your long code to run. To schedule your heavy code on the next run loop iteration and let the controller be dismissed, use a 0ms delay to perform it.
I would change your code to this: