执行代码/ after / UIImagePicker被解雇

时间:2010-08-22 19:41:41

标签: iphone uiimagepickercontroller

我有一个简单的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在视觉上被解除。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

我意识到这不是最优雅的答案。我之前遇到过类似的问题(试图在viewDidAppear中做视觉事情但是没有用)。我发现通过调用performSelector:withObject:afterDelay:可以看到我的视觉效果。

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

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

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

答案 1 :(得分:1)

没有必要使用背景方法,但您可以尝试这种方法:

- (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]];
}

答案 2 :(得分:0)

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];

答案 3 :(得分:0)

我认为你不会挂在你认为自己的位置。据我所知,UIImagePNGRepresentation需要相当长的时间来执行(UIImageJPEGRepresentation要快得多,如果你不介意没有无损)。您可以将字典传递给您的方法并获取您需要的NSData。

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

我会将您的代码更改为:

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