2012-11-05 20 views
2

我正在嘗試一個簡單的相機應用程序的實踐。在下面這些例子:iOS 6 AppiOS 4 App從應用程序本身訪問相機

我的代碼如下所示:

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 
{ 
    UIButton *button; 
    UIImageView *imageView; 
} 
@property (nonatomic, retain) IBOutlet UIImageView *imageView; 
- (IBAction)useCamera; 
- (IBAction)useCameraRoll; 
@end 

ViewController.m

#import "ViewController.h" 
#import <MobileCoreServices/MobileCoreServices.h> 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize imageView; 


- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // After saving iamge, dismiss camera 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 
{ 
    UIAlertView *alert; 

    // Unable to save the image 
    if (error) 
     alert = [[UIAlertView alloc] initWithTitle:@"Error" 
              message:@"Unable to save image to Photo Album." 
              delegate:self cancelButtonTitle:@"Ok" 
           otherButtonTitles:nil]; 
    else // All is well 
     alert = [[UIAlertView alloc] initWithTitle:@"Success" 
              message:@"Image saved to Photo Album." 
              delegate:self cancelButtonTitle:@"Ok" 
           otherButtonTitles:nil]; 


    [alert show]; 
} 

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    // Access the uncropped image from info dictionary 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    // Save image 
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 

} 

- (id)init 
{ 
    if (self = [super init]) 
    { 
     self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
     self.view.backgroundColor = [UIColor grayColor]; 

     // Button to activate camera 
     button = [[UIButton alloc] initWithFrame:CGRectMake(80, 55, 162, 53)]; 
     [button setBackgroundImage:[UIImage imageNamed:@"Camera.png"] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside]; 
     [self.view addSubview:button]; 
    } 

    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)useCamera 
{ 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypeCamera]) 
    { 
     UIImagePickerController *imagePicker = 
     [[UIImagePickerController alloc] init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypeCamera; 
     imagePicker.mediaTypes = [NSArray arrayWithObjects: 
            (NSString *) kUTTypeImage, 
            nil]; 
     imagePicker.allowsEditing = NO; 
     [self presentViewController:imagePicker animated:YES completion:nil]; 

    } 
} 

- (IBAction)useCameraRoll 
{ 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypeSavedPhotosAlbum]) 
    { 
     UIImagePickerController *imagePicker = 
     [[UIImagePickerController alloc] init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypePhotoLibrary; 
     imagePicker.mediaTypes = [NSArray arrayWithObjects: 
            (NSString *) kUTTypeImage, 
            nil]; 
     imagePicker.allowsEditing = NO; 
     [self presentViewController:imagePicker animated:YES completion:nil]; 
    } 
} 

@end 

我的問題,我想訪問相機從應用程序本身。會發生什麼,當我點擊useCamera按鈕時,它會打開相機並拍攝圖像。它可能會從應用程序本身做到這一點嗎?對不起,如果這是一個愚蠢的問題。如果我做錯了什麼,請指出我。

+0

嗨,你想要做的是在你的應用程序中打開uiimagepicker?保存導航控制器(例如)和tabbarcontroller(例如)? –

+0

雅...它似乎走出應用程序做照片拍攝...保存導航控制器部分,不... – lakesh

+0

我不知道,但而不是presentViewController,你可以使[self.view addsubview:youpicker.view]否?所以你可以自定義你的選擇框架等等... –

回答

2

是的,請使用UIImagePickerController class

單擊相機按鈕時,顯示UIImagePickerController的實例。此外,由於一些iOS設備(如原始iPad或較舊的iPod touch)沒有相機,因此我不建議使用單獨的按鈕/方法拍攝新照片,而使用保存的照片中的一張。使用一個按鈕並使用該類中的可用方法根據設備的功能設置其源類型。它可能需要重新考慮你的應用程序的設計,但它也更有可能通過蘋果的UI檢查,因爲它們可能不會批准具有僅用於拍照的按鈕但可以在沒有照相機的設備上運行的應用程序。

一旦用戶拍攝了新照片或者從相機膠捲中選擇了照片,請使用UIImagePickerControllerDelegate class中的imagePickerController:didFinishPickingMediaWithInfo:方法將用戶的選擇傳遞給您的代碼。

UIImagePickerController的實例旨在在使用相機時在所有iOS設備上進行模態呈現。請記住,顧名思義,它們是控制器和非視圖,因此不應將它們用作UIView的子視圖。雖然可能通過展示方式獲得創意,但幾乎肯定會有一些副作用,並且會進一步增加蘋果拒絕它的機會。它只是像它拿出應用程序來拍攝照片一樣,但因爲您是從應用程序中呈現控制器,所以它仍然是其中的一部分。蘋果不允許直接訪問攝像頭硬件,這正是他們創建此課程的原因。

相關問題