UIImagePicker视图在首次使用后不会自行删除

时间:2011-02-14 02:01:23

标签: iphone uiimagepickercontroller

我有一个应用程序正在添加一个用于调用UIImagePickerController的视图。当使用点击添加图像按钮时,执行以下代码:

' - (IBAction)addPhoto:(id)sender {

// Call background tap in case any keyboards are still up
[self backgroundTap:sender];

if (jpegData) {

    // If we already chose an image, don't allow to choose another.
    // Have to cancel out and come back!
    return;
}

// Shows the photo picker so the user can take or select an image
photoPickerViewController = [[PhotoPickerViewController alloc] initWithNibName:@"PhotoPicker" bundle:nil];
photoPickerViewController.delegate = self;

// Add it to the subview - it will auto animate in/out
[self.view addSubview:photoPickerViewController.view];

}

这向用户显示我创建的具有3个按钮的视图:拍照,选择现有照片和取消。取消只是取消回主视图。如果调用照片或选择现有,则执行此代码:

' - (IBAction)choosePhoto:(id)sender {

// Show an image picker to allow the user to choose a new photo.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

if((UIButton*)sender == chooseExistingButton) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = YES;
}

[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];

}

如果用户从图像选择器中取消,我们将返回主视图。没问题。然而,如果他们完成图像选择(通过拍照或选择现有图像),我们会打电话:

' - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

// Ok, dismiss the modal picker, bring up the activity indicator and dispatch a thread
// that will do all the photo processing ...
BOOL isFromCamera = picker.sourceType == UIImagePickerControllerSourceTypeCamera;

// Dismiss the picker view controller
[picker dismissModalViewControllerAnimated:NO];

// And remove our view from the list of views
[self.view removeFromSuperview];

if (isFromCamera)
{       
        // Declare the completion block to use
    ALAssetsLibraryWriteImageCompletionBlock compBlock = ^(NSURL *assetURL, NSError *error) {

        if (error != nil || assetURL == nil) {

            NSLog(@"Failed to save photo: %@", error);
            [delegate photoSetURLForImage:nil];
        }

        else {

            NSLog(@"URL is : %@", [assetURL absoluteString]);
            [delegate photoSetURLForImage:assetURL];
        }
    };

    ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
    [library writeImageToSavedPhotosAlbum:cgimage
                                 metadata:meta
                          completionBlock:compBlock];
    return;
}
else {
    // Use the URL to get the metadata for the image that was picked ...
    NSURL* url = [(NSDictionary*)info objectForKey:@"UIImagePickerControllerReferenceURL"];

    if (url) {

        // Define a result and failure block for fetching from the ALAsset
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset* myasset)
        {
            ALAssetRepresentation *rep = [myasset defaultRepresentation];

            NSLog(@"URL is : %@", [[rep url] absoluteString]);
            [delegate photoSetURLForImage:[rep url]];
        };

        // And also define a failure block
        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
        {
            [delegate photoSetURLForImage:nil];
        };

        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:url 
                       resultBlock:resultblock
                      failureBlock:failureblock];
        return;
    }
}

// If we get here, something went very wrong
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"An error occured while retrieving the image" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alert show];

}

现在,我第一次浏览此代码时,一切都很棒。一旦我在相机拍摄的图像上点击“使用”,或者我从相机胶卷中选择了一个图像,UIImagePicker视图和启动它的视图就会消失,我等待时我回到主视图对于ALAsset调用来做他们的事情。

当我尝试第二次重复此过程时,问题就出现了。选择图像或取一个图像后,处理开始,但视图不会消失,直到ALAsset调用的所有处理完成。我无法弄清楚为什么会这样。为什么它第一次工作,但不是在任何时候。我必须清除一些缓存机制吗?无论如何,如果有人能提供一些建议,我将非常感激。

谢谢,

Ĵ

1 个答案:

答案 0 :(得分:0)

然而,似乎我将回答我自己的问题。这开始变成习惯了。

所以似乎ALAsset调用没有旋转出一个我认为应该做的新线程。我的错。所以要解决,这只是产生一个新线程来完成所有ALAsset的事情,并在主线程中解雇我的选择器而不是。

问题解决了,耶!