开发者社区> 问答> 正文

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

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

展开
收起
杨冬芳 2016-07-01 16:33:37 2307 0
1 条回答
写回答
取消 提交回答
  • IT从业
    - (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
    从摄像头/相册获取图片

    2019-07-17 19:50:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载