iOS下关于如何调用本地相机

iOS下关于如何调用本地相机和如何保存照片到本地相册:

本实例中,使用storyboard拖拽的控件,参考者可以自己选择方式:

使用stroyboard可以提高代码效率,用于快速出成品展示:

在.m中:

#import "ViewController.h"


@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imagePicture;


@end


@implementation ViewController

- (IBAction)saveClick:(id)sender

{

    //UIImageWriteToSavedPhotosAlbum(self.imagePicture.image, nil, nil, nil);

    UIImageWriteToSavedPhotosAlbum(self.imagePicture.image, self, @selector(image:didFinshSavingWithError:contextInfo:), nil);

//    UIAlertView *alertPhoto = [[UIAlertView alloc]initWithTitle:@"" message:@"已保存到相册"

//                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

//    [alertPhoto show];

    

    

}

- (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    NSString *mes = nil;

    if (error == nil) {

        mes = @"保存成功";

        

    }else

    {

        mes = @"保存失败";

    }

    UIAlertView *alertPhoto = [[UIAlertView alloc]initWithTitle:@"提醒" message:mes

                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alertPhoto show];


}


//打开照相机的方法

- (IBAction)cameraClick:(id)sender

{

    //调用相机

    //diaoy xiangji

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

    

    //判断当前设备是否有照相功能

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        //判断如果没有相机就调用图片库

        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    }

    

    UIImagePickerController *picker = [[UIImagePickerController alloc]init];

    picker.sourceType = sourceType;

    picker.delegate = self;

    [self presentViewController:picker animated:YES completion:^{

        

    }];

}

//点击相册的选择按钮,  或者  相机照完之后的使用按钮

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //把选择的图片给imageView

    self.imagePicture.image = [info valueForKey:UIImagePickerControllerOriginalImage];

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

//点击取消按钮的时候触发

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}



//打开相册

- (IBAction)photoClick:(id)sender

{

    //初始化

    UIImagePickerController *picker= [[UIImagePickerController alloc]init];

    //选择调用的类型

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    //推出相册界面

    [self presentViewController:picker animated:YES completion:^{

        

    }];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值