我有一個應用程序添加了一個用於調用UIImagePickerController的視圖。當使用點擊該加載圖像按鈕時,下面的代碼執行:UIImagePicker視圖第一次使用後不會自行刪除
「 - (IBAction爲)addPhoto:(ID)發送方{
// 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)發送方{
// 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];
}
如果用戶從抵消當圖像選擇器,我們回到主視圖。沒問題。但是,如果他們完成圖像選擇(通過拍照或選擇一個現有的),那麼我們致電:
「 - (無效)imagePickerController:(的UIImagePickerController *)選擇器didFinishPickingMediaWithInfo:(NSDictionary的*)信息{
// 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調用的所有處理都完成爲止。我無法弄清楚這是爲什麼。爲什麼它第一次運作,但不是在任何時候。是否有一些緩存機制需要清除?無論如何,如果有人能提供一些建議,我會非常感激。
感謝,
Ĵ