1
我寫了一段代碼,它在基於PhoneGap的應用程序中運行。該代碼有一個插件,它可以在使用以下代碼單擊網頁上的某個綁定按鈕時啓動UIImagePickerController。iOS UIImagePickerController和ModalViewController
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一樣呈現。任何想法爲什麼?
這將工作,但請告訴我如何呈現多個模態視圖一個接一個地解散。 – Neutralizer 2011-04-17 18:14:26
你爲什麼想這麼做?您可以通過冷靜觀看控制器而不使用動畫並使用動畫呈現另一個來嘗試此操作。 – Ravin 2011-04-18 03:15:59
實際上,網頁上的一個按鈕允許用戶選擇哪個本地代碼必須通過HTTP上傳到某個服務器。一種模式的觀點是讓用戶選擇照片,另一種是爲應用程序上傳選擇的照片,顯示「照片正在上傳......」 – Neutralizer 2011-04-18 15:55:10