其实照相机的代码,网络里已经满天飞了,几乎不用怎么再看,现在就稍微记录一下
<span style="font-size:18px;">- (IBAction)takeImage:(id)sender { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {//判断是否可用 _imagePicker = [[UIImagePickerController alloc] init]; _imagePicker.delegate = self; _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//设置类型 [self presentViewController:_imagePicker animated:YES completion:nil]; } else { NSLog(@"照相机不可用。"); } } - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker { [self dismissViewControllerAnimated:YES completion:nil]; } - (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info { UIImage *originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage]; self.imageView.image = originalImage; self.imageView.contentMode = UIViewContentModeScaleAspectFill; [self dismissViewControllerAnimated:YES completion:nil]; }</span>
didFinishPickingMediaWithInfo: (NSDictionary *) info里的信息
例如:
UIImagePickerControllerOriginalImage 获取原始图片信息键
UIImagePickerControllerEditedImage 编辑后的图片数据键
UIImagePickerControllerMediaType 由用户指定的媒体类型键
UIImagePickerControllerCropRect 剪裁后的图片数据键
UIImagePickerControllerMediaURL 视频存放路径键
UIImagePickerControllerReferenceURL 原始选择项的URL
UIImagePickerControllerMediaMetadata 只有在使用摄像头并且是图像类型的时候有效.包含选择图像信息的字典类型
还有一个要注意的就是保存图片的方法
<span style="font-size:18px;">UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); </span>
<span style="font-size:18px;"> - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { UIAlertView *alert; if (error) { alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"保存图片失败。" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; } else { alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"保存图片成功。" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; } [alert show]; [self dismissViewControllerAnimated:YES completion:nil]; } </span>
利用UIImagePickerControllerReferenceURL 我们也可以进行以下操作
<span style="font-size:18px;">self.item.backgroundImg = [NSString stringWithFormat:@"%@",[info objectForKey:UIImagePickerControllerReferenceURL]] ; [self.libaray assetForURL:[NSURL URLWithString:self.item.backgroundImg] resultBlock:^(ALAsset *asset){ //获取资源图片的详细资源信息 ALAssetRepresentation* representation = [asset defaultRepresentation]; //获取资源图片的高清图 image = [UIImage imageWithCGImage:[representation fullScreenImage]]; if (!image) { image = [UIImage imageNamed:@"1.jpg"]; } self.backGroundImageView.image = image; } failureBlock:^(NSError *error){ }]; </span>
<span style="font-size:18px;"> _imagePicker.cameraOverlayView = [self customOverlayView]; //在相机的overlayView上添加图层 _imagePicker.showsCameraControls = NO;//隐藏照相机控件按钮 CGFloat camScaleup = 1.8;//放大照相机图层 _imagePicker.cameraViewTransform = CGAffineTransformScale(_imagePicker.cameraViewTransform, camScaleup, camScaleup);</span>
这样你在想要的方法里,添加想要的图层就可以了,我想水印相机就是这个原理吧!
通过AVFoundation 捕获视频
//实例化视频捕获对象 AVCaptureSession *session = [[AVCaptureSession alloc]init]; //设置画质 session.sessionPreset = AVCaptureSessionPresetMedium; //创建视频图层 AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session]; captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 568); //设置缩放比例 CGFloat camScaleup = 1.8; captureVideoPreviewLayer.affineTransform = CGAffineTransformMakeScale(camScaleup, camScaleup); [self.view.layer addSublayer:captureVideoPreviewLayer]; //指定媒体类型获得默认捕获设备AVCaptureDevice对象 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; //实例化捕获输入对象 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) { NSLog(@"%@",error); } //加入捕获会话中 [session addInput:input]; //实例化静态图像输出对象 AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc]init]; //设置输出参数 NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey ,nil]; stillImageOutput.outputSettings = dict; [session addOutput:stillImageOutput]; [session startRunning]; // 创建执行动画的Image View对象 UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; NSMutableArray *array1 = [[NSMutableArray alloc] init]; NSMutableArray *array2 = [[NSMutableArray alloc] init]; for (int i =10; i <= 40; i++) { NSString *fileName1 = [[NSString alloc] initWithFormat:@"100%i.png",i]; [array1 addObject:[UIImage imageNamed:fileName1]]; NSString *fileName2 = [[NSString alloc] initWithFormat:@"100%i.png",(50-i)]; [array2 addObject:[UIImage imageNamed:fileName2]]; } [array1 addObjectsFromArray:array2]; imageView.animationImages = array1; imageView.animationDuration = 1.75; imageView.animationRepeatCount = 0; // start animating [imageView startAnimating]; [self.view addSubview:imageView];
更多干货,请支持原作:http://item.jd.com/11436547.html