美文网首页
自定义相机裁剪图片

自定义相机裁剪图片

作者: 新生的光明 | 来源:发表于2018-10-18 19:53 被阅读0次

    目前正好需要做裁剪图片,且支持相机拍摄后,按照规定大小裁剪,这里记录一下

    首先初始化摄像头

        self.session = [[AVCaptureSession alloc] init];
        self.session.sessionPreset = AVCaptureSessionPresetHigh;
        NSError *error;
        
        self.effectiveScale = 1.0;
        
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        [device lockForConfiguration:nil];
        if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
            [device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
        }
        
        [device unlockForConfiguration];
        
        self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
        if (error) {
            NSLog(@"%@",error);
        }
        self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
        //输出设置。AVVideoCodecJPEG   输出jpeg格式图片
        NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
        [self.stillImageOutput setOutputSettings:outputSettings];
        
        if ([self.session canAddInput:self.videoInput]) {
            [self.session addInput:self.videoInput];
        }
        if ([self.session canAddOutput:self.stillImageOutput]) {
            [self.session addOutput:self.stillImageOutput];
        }
        
        //初始化预览图层
        self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
        [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        
        self.previewLayer.frame = CGRectMake(0, 0,kGZIM_SCREEN_WIDTH, kGZIM_SCREEN_HEIGHT);
        self.view.layer.masksToBounds = YES;
        [self.view.layer addSublayer:self.previewLayer];
    

    点击拍照后,用下面的方式获取图片

     - (void)takePhotoButtonClick {
        _stillImageConnection = [self.stillImageOutput        connectionWithMediaType:AVMediaTypeVideo];
        UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
        AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];
        [_stillImageConnection setVideoOrientation:avcaptureOrientation];
        
        [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:_stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
            
            NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            
            UIImage *img = [UIImage imageWithData:jpegData];
            
            NSLog(@"%f   %f",img.size.width,img.size.height);
            
            img = [self image:img scaleToSize:CGSizeMake(kGZIM_SCREEN_WIDTH, kGZIM_SCREEN_HEIGHT)];
            //CGRectMake(30, 200, kDeviceWidth-60, kDeviceHeight-200-300)
            CGRect rc = self.clipRect;
            rc.origin.x *= 2;
            rc.origin.y *= 2;
            rc.size.width *= 2;
            rc.size.height *= 2;
            img = [self imageFromImage:img inRect:rc];
        }];
    }
    
    
    -(UIImage*)image:(UIImage *)imageI scaleToSize:(CGSize)size{
        UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
        [imageI drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return scaledImage;
    }
    
    
    -(UIImage *)imageFromImage:(UIImage *)imageI inRect:(CGRect)rect{
        CGImageRef sourceImageRef = [imageI CGImage];
        CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
        return newImage;
    }
    
    

    到此就结束了,最后附上一个截图


    21539863423_.pic.jpg

    demo放在后面的从相册选择图片并裁剪的文章中

    相关文章

      网友评论

          本文标题:自定义相机裁剪图片

          本文链接:https://www.haomeiwen.com/subject/nowszftx.html