ios添加图片为什么从相册里面就可以,从摄像头上就不行,小白求解答-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

ios添加图片为什么从相册里面就可以,从摄像头上就不行,小白求解答

杨冬芳 2016-07-01 16:33:37 1469

-(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

iOS开发
分享到
取消 提交回答
全部回答(1)
  • 杨冬芳
    2019-07-17 19:50:13
    - (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
    从摄像头/相册获取图片

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题