执行代码 /after/ UIImagePicker 被解雇

发布于 2024-09-15 03:30:46 字数 613 浏览 2 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏爱你一生 2024-09-22 03:30:46

我意识到这不是最优雅的答案。我以前也遇到过类似的问题(尝试在 viewDidAppear 中执行视觉操作但不起作用)。我发现通过调用performSelector:withObject:afterDelay:导致我看到了视觉效果。

所以在你的情况下,它会是:

[self performSelector:@selector(executeSomeCode:) withObject:data afterDelay:0];

我不知道这是否适合你(或者为什么它对我有用)。

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:

[self performSelector:@selector(executeSomeCode:) withObject:data afterDelay:0];

I have no idea if this will work for you (or why it worked for me).

风向决定发型 2024-09-22 03:30:46

不必使用后台方法,但您可以尝试以下方法:

- (void)_processImage:(UIImage*)image 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // convert to PNG data here, etc.
    // this is background, remember to manipulate UI in main thread
    // Example: [self.anImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    [pool release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    UIImage *picked = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [self performSelectorInBackground:@selector(_processImage:) withObject:[[picked retain] autorelease]];
}

It shouldn't be necessary to use a background method, but you can try this approach:

- (void)_processImage:(UIImage*)image 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // convert to PNG data here, etc.
    // this is background, remember to manipulate UI in main thread
    // Example: [self.anImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    [pool release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    UIImage *picked = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [self performSelectorInBackground:@selector(_processImage:) withObject:[[picked retain] autorelease]];
}
驱逐舰岛风号 2024-09-22 03:30:46

http://developer.apple.com/ iphone/library/documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html

我相信您的调用在 iOS3 中已被弃用

您可以尝试

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

在关闭 viewController 之前从图像中获取数据

NSData *data = UIImageJPEGRepresentation( [info objectForKey:@"UIImagePickerControllerOriginalImage"] , 1.0);
[self dismissModalViewControllerAnimated:TRUE];
[self executeSomeCode:img];

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

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

and the get the data from the image before dismissing the viewController

NSData *data = UIImageJPEGRepresentation( [info objectForKey:@"UIImagePickerControllerOriginalImage"] , 1.0);
[self dismissModalViewControllerAnimated:TRUE];
[self executeSomeCode:img];
酷到爆炸 2024-09-22 03:30:46

我不认为你挂在你认为的地方。据我所知, UIImagePNGRepresentation 需要相当长的时间才能执行(如果您不介意不无损的话,UIImageJPEGRepresentation 要快得多)。您可以将字典传递到您的方法中并在其中获取您需要的 NSData。

你的第二个问题是你的代码是同步的,无论你多久告诉你的控制器关闭,它都会等待你的长代码运行。要在下一次运行循环迭代中安排繁重的代码并让控制器关闭,请使用 0ms 延迟来执行它。

我会将你的代码更改为:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [self performSelector:@selector(executeSomeCode:) withObject:info afterDelay:0.0f];
}

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:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [self performSelector:@selector(executeSomeCode:) withObject:info afterDelay:0.0f];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文