-(void)showImagePicker{
UIApplication *myApp = [UIApplication sharedApplication];
[myApp setStatusBarHidden:YES];//先把状态栏隐藏起来
UIImagePickerController* picker = [[UIImagePickerController alloc]init];//创建
picker.delegate = self;//设置为托
// picker.view.frame=CGRectMake(0, 0, 320, 460);//重绘大小与位置
// picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//从相册中获取
picker.sourceType = UIImagePickerControllerSourceTypeCamera; //从照相机获取
picker.allowsEditing=YES;//允许编辑图片
[self.view addSubview:picker.view];//添加到当前窗口
为什么从相册中获取相片就可以,而从摄像头获取照片就不行一直出断点,求大神指点,本人小白
Stard
- (void) openCamera {
NSLog(@"********** open camera");
AVCaptureSession *session = [[[AVCaptureSession alloc] init] autorelease];
session.sessionPreset = AVCaptureSessionPreset640x480;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
// 创建数据输入来源
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (input) {
// 添加接口
[session addInput:input];
stillImageOutput = [[[AVCaptureStillImageOutput alloc] init] autorelease];
NSDictionary *outputSettings = [[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil] autorelease];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
//宣告一個AVCaptureVideoPreviewLayer的指標,用來存放session要輸出的東西(Camera中的畫面)
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
//設定輸出畫面的邊界大小
previewLayer.frame = CGRectMake(0, 54, 320, 372);
//將輸出的東西透過preview秀出來,preview為UIImageView型態
[self.view.layer addSublayer:previewLayer];
//完成session的輸入端與輸出端的相關設定之後,啟動的這個session,它將會自動將輸入端與輸出端做連結
[session startRunning];
}else {
NSLog(@"********** camera got Error:");
}
}
// 拍照
- (IBAction) shot:(id)sender {
NSLog(@"********** take a shot");
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
// NSLog(@"about to request a capture from:%@",stillImageOutput);
// block方法 & 记得 #import <ImageIO/ImageIO.h>
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:
^(CMSampleBufferRef imageSampleBuff, NSError *error){
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuff, kCGImagePropertyPNGDictionary, NULL);
if (exifAttachments) {
// NSLog(@"attachments :%@",exifAttachments);
}else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuff];
UIImage *image = [UIImage imageWithData:imageData];
NSLog(@"----%f, %f",image.size.width, image.size.height);
}
];
}
http://jingyan.baidu.com/article/e73e26c0bcb06824adb6a785.htmlios
从摄像头/相册获取图片
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。