此範例為點及按鈕開啟相機,

使用相機拍照顯示在imageview後並存入相簿



#import "ViewController.h" @interface ViewController () - (IBAction)takePictureButton:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *myImageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITapGestureRecognizer * tapPress = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; tapPress.numberOfTapsRequired = 1; [_myImageView setUserInteractionEnabled:YES]; [_myImageView addGestureRecognizer:tapPress]; } -(void)handleTap:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"cancle" destructiveButtonTitle:nil otherButtonTitles:@"save to album", nil]; [actionSheet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex == 0 ) { UIImageWriteToSavedPhotosAlbum( _myImageView.image , self, @selector(image:didFinishSavingWithError:comtextInfo:), nil); } } -(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if(!error){ UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"save to album" message:@"save to album already" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alertView show]; }else{ UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"save to album" message:@"failed to save" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alertView show]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)takePictureButton:(id)sender { UIImagePickerController * ipc = [[UIImagePickerController alloc]init]; //確認裝置是否有相機可使用 if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) { ipc.sourceType = UIImagePickerControllerSourceTypeCamera; //確認是否有後方鏡頭 if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) { ipc.cameraDevice = UIImagePickerControllerCameraDeviceRear; //設定是拍照還是錄影 ipc.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; ipc.allowsEditing = YES; ipc.delegate = self; [self presentViewController:ipc animated:YES completion:nil]; } } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //取得目前拿到的資料型態 NSString *mediaType = [ info objectForKey:UIImagePickerControllerMediaType]; //目前照相的畫面關閉 [picker dismissViewControllerAnimated:YES completion:nil]; //資料型態必須為一般的照片 if([mediaType isEqualToString:@"public.image"]) { //照片是否編輯過 if([info objectForKey:UIImagePickerControllerEditedImage]) { UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; _myImageView.image = editedImage; }else{ UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; _myImageView.image = originalImage; } } } @end
arrow
arrow
    文章標籤
    使用相機拍照並存入相簿
    全站熱搜
    創作者介紹
    創作者 Will 的頭像
    Will

    Will的部落格

    Will 發表在 痞客邦 留言(0) 人氣()