【问题标题】:crop an image taken with the camera in swift快速裁剪用相机拍摄的图像
【发布时间】:2015-08-05 02:43:10
【问题描述】:

我有一个应用程序,用户可以在其中拍照,但我只需要保存我用作蒙版的 UIView 区域(背景清晰),当我尝试进行裁剪时,这是错误的,我怀疑图像大于屏幕和裁剪区域它工作良好,但参考图像而不是屏幕区域。

这是我的代码:(cameraFrame 是蒙版视图)

if let videoConnection = output.connectionWithMediaType(AVMediaTypeVideo) {
            output.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                var cropped = CGImageCreateWithImageInRect(cgImageRef, self.cameraFrame.frame)
                self.imagen = UIImage(CGImage: cropped, scale: 1.0, orientation: UIImageOrientation.Right)
                self.performSegueWithIdentifier("aImagen", sender: self)
            })
        }

这是正在运行的应用程序:

【问题讨论】:

  • 这正是我现在想要实现的。你解决了吗?
  • @Nevermore 你找到解决方案了吗?对于更多的观众,不幸的是,答案中提到的 GitHub 项目仅支持 Swift 3.1...

标签: ios swift camera


【解决方案1】:

我正在使用以下 pod

https://github.com/AlexLittlejohn/ALCameraViewController

用法

let croppingEnabled = true
let cameraViewController = ALCameraViewController(croppingEnabled: croppingEnabled) { image in
    // Do something with your image here. 
    // If cropping is enabled this image will be the cropped version
}

presentViewController(cameraViewController, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多