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

自定义相机裁剪图片

作者: 新生的光明 | 来源:发表于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放在后面的从相册选择图片并裁剪的文章中

相关文章

  • 自定义相机裁剪图片

    目前正好需要做裁剪图片,且支持相机拍摄后,按照规定大小裁剪,这里记录一下 首先初始化摄像头 点击拍照后,用下面的方...

  • 开发常用第三方库

    自定义tabbar; 无限循环图片,文字轮播器; 自定义statusBar 自定义搜索界面 图片裁剪 启动广告 自...

  • Android开发galleryfinal裁剪png图片问题

    galleryfinal裁剪png图片问题## galleryfinal是第三方开源框架,相册浏览,裁剪,相机,多...

  • Android GitHud 项目插件

    1、android自定义相册,实现了拍照、图片选择(单选/多选)、 裁剪(单/多裁剪)、旋转、ImageLoade...

  • Quartz2D 实例应用

    裁剪一个圆形的图片的分类 截图 调用 自定义图片的截取 擦除图片

  • Android 相机/相册 图片裁剪

    打开相机 打开相册 **使用 uCrop 裁剪 详情请参考 Android 头像选择(拍照、相册裁剪),含7.0的...

  • Flutter 图片裁剪(相机,相册)

    今天用到了一个特别好用的图片裁剪库,在这里分享一下Copper 先看一下效果图 这个裁剪库还是蛮强的,有左右旋转,...

  • 仿微信图片选择

    Android图片选择器,仿微信的图片选择器的样式和效果。支持图片单选、多选、裁剪形状自定义、裁剪比例设置、解耦图...

  • Android多图选择工具

    ImagePicker Android 图片选择工具,支持单选/多选/裁剪/预览/相机拍摄,图片加载可以自由选择G...

  • iOS 图片裁剪

    图片裁剪根据需求自行设置newSize如果是等比例裁剪(宽高比不变的情况下),可将从相册或者相机拿到的图片获取im...

网友评论

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

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