Я написал фрагмент кода, который работает внутри приложения на основе PhoneGap.В коде есть плагин, который запускает UIImagePickerController при нажатии какой-либо связанной кнопки на веб-странице с использованием следующего кода.
PhotoSelectorCommand.h
#import <Foundation/Foundation.h>
#import "PhoneGapCommand.h"
@interface PhotoSelectorCommand : PhoneGapCommand<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
NSString *url;
NSString *extra;
}
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *extra;
- (void) startPhotoSelector:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
@end
PhotoSelectorCommand.m
#import "PhotoSelectorCommand.h"
#import "PhoneGapViewController.h"
#import "PhotoUploaderViewController.h"
@implementation PhotoSelectorCommand
@synthesize url;
@synthesize extra;
- (void) startPhotoSelector:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url
{
NSUInteger argc = [arguments count];
if (argc < 1) {
return;
}
self.url = [arguments objectAtIndex:0];
if (argc > 1) {
self.extra = [arguments objectAtIndex:1];
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
PhoneGapViewController* cont = (PhoneGapViewController *)[super appViewController];
[cont presentModalViewController:imagePicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//on selected
[picker dismissModalViewControllerAnimated:YES];
PhotoUploaderViewController *photoUploader = [PhotoUploaderViewController alloc];
PhoneGapViewController* cont = (PhoneGapViewController *)[super appViewController];
[cont presentModalViewController:photoUploader animated:YES];
[photoUploader.view removeFromSuperview];
[picker release];
[picker.view removeFromSuperview];
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//on cancel
NSString *failureCallback = [NSString stringWithFormat:@"PhotoSelector._onError('%@');", @"No image selected."];
[webView stringByEvaluatingJavaScriptFromString:failureCallback];
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
}
@end
PhotoSelectorViewController.h
#import <UIKit/UIKit.h>
@interface PhotoUploaderViewController : UIViewController {
}
@end
PhotoSelectorViewController.m
#import "PhotoSelectorViewController.h"
@implementation PhotoSelectorViewController
-(void)init {
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
@end
Приведенный выше код работает, пока не покажет UIImagePickerController, где пользователь может выбрать изображение.Один раз после выбора любого изображения контроллер отключается, но другой вид не отображается, как UIImagePickerController.Есть идеи почему?