拍摄相片

iOS拍照功能实现
本文介绍如何在iOS应用中实现拍照功能,包括检查相机可用性、使用UIImagePickerController进行拍照、编辑及保存照片到相册等关键步骤。

<UIImagePickerControllerDelegate>


- (IBAction)takePicture:(id)sender { // Make sure camera is available //UIImagePickerControllerSourceTypePhotoLibrary,访问设备上保存的所有照片 //UIImagePickerControllerSourceTypeCamera, //UIImagePickerControllerSourceTypeSavedPhotosAlbum只能用于访问Camera Roll相册 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Camera Unavailable" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; [alert show]; return; } if (self.imagePicker == nil) { self.imagePicker = [[UIImagePickerController alloc] init]; self.imagePicker.delegate = self; self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; self.imagePicker.allowsEditing = YES;//可编辑状态 } [self presentViewController:self.imagePicker animated:YES completion:NULL]; }
//取回相片、更新图像视图,隐藏图像选取器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];//可编辑状态
    UIImageWriteToSavedPhotosAlbum (image, nil, nil , nil);//照片保存到相册中
    self.imageView.image = image;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;//充满整个视图

//self.imageView.contentMode = UIViewContentModeScaleAspectFit;//固定纵横比显示整张图片

[self dismissViewControllerAnimated:YES completion:NULL];
}
//取消图片选择,隐藏图片选择器
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

 

转载于:https://www.cnblogs.com/fengmin/p/5520642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值