iOS相机拍摄的调整后的图像在拍摄后不会显示为

时间:2016-03-17 01:14:16

标签: ios swift camera

我的问题是,在拍摄完图像后,它会自动缩放,因此通过查看相机视图,它看起来与我所期望的完全不同。

为了处理相机工作,我使用的是开源库,一个名为CameraManager的pod。您可以在下面找到我的代码,稍后我将解释图像如何自我缩放。

import UIKit
import CameraManager

class ViewController: UIViewController {
    @IBOutlet weak var height: NSLayoutConstraint!
    @IBOutlet weak var width: NSLayoutConstraint!
    @IBOutlet weak var cameraView: UIView!

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var imageHeight: NSLayoutConstraint!
    @IBOutlet weak var imageWidth: NSLayoutConstraint!

    let cameraManager = CameraManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.hidden = true

        height.constant = self.view.frame.width
        width.constant = self.view.frame.width

        imageHeight.constant = self.view.frame.width
        imageWidth.constant = self.view.frame.width

        cameraManager.addPreviewLayerToView(self.cameraView)
        cameraManager.writeFilesToPhoneLibrary = false
    }

    @IBAction func shootImage(sender: AnyObject) {
        print("bastim")
        print("self.imageView.frame.height \(self.cameraView.frame.height) and self.imageView.frame.width \(self.cameraView.frame.width)")
        print("camera x \(self.cameraView.frame.origin.x) and camera y \(self.cameraView.frame.origin.y)")

        cameraManager.capturePictureWithCompletition({ (image, error) -> Void in
            var ratio: CGFloat
            var delta: CGFloat
            var offset: CGPoint

            let size: CGSize = CGSizeMake(self.cameraView.frame.width, self.cameraView.frame.height)

            if(image?.size.width > image?.size.height) {
                ratio = self.cameraView.frame.width / image!.size.width
                delta = ratio*image!.size.width - ratio*image!.size.height
                offset = CGPointMake(delta/2, 0)
            }else {
                ratio = self.cameraView.frame.width / image!.size.height
                delta = ratio*image!.size.height - ratio*image!.size.width
                offset = CGPointMake(0, delta/2)
            }
            print("delta \(delta)")

            let clipRect: CGRect = CGRectMake(-offset.x, -offset.y, (ratio * image!.size.width) + delta, (ratio * image!.size.height) + delta)

            if (UIScreen.mainScreen().respondsToSelector(Selector("scale")) && UIScreen.mainScreen().scale >= 2.0) {
                UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
            }else {
                UIGraphicsBeginImageContext(size)
            }

            UIRectClip(clipRect)
            image?.drawInRect(clipRect)
            let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            self.cameraView.hidden = true
            self.imageView.hidden = false
            self.imageView.image = newImage
        })
    }

    @IBAction func repeatProcess(sender: AnyObject) {
        self.cameraView.hidden = false
        self.imageView.hidden = true
    }

}

下面你可以详细找到我的问题。

可能缩放错误或者可能裁剪错误的尺寸。我不确定哪两个是正确的。说实话,我有点迷茫,在这里找不到问题。

2 个答案:

答案 0 :(得分:0)

终于得到了答案!遇到同样问题的人请参考下面的代码。这对我很有用:

 @IBAction func shootImage(sender: AnyObject) {

    print("self.cameraView.frame.height \(self.cameraView.frame.height) and self.cameraView.frame.width \(self.cameraView.frame.width)")
    print("camera x \(self.cameraView.frame.origin.x) and camera y \(self.cameraView.frame.origin.y)")

    print("self.imageView.frame.height \(self.imageView.frame.height) and self.imageView.frame.width \(self.imageView.frame.width)")
    print("image view x \(self.imageView.frame.origin.x) and image view y \(self.imageView.frame.origin.y)")


    cameraManager.capturePictureWithCompletition({ (image, error) -> Void in
        let scaleTransform: CGAffineTransform!
        let origin: CGPoint

        if(image!.size.width > image!.size.height) {
            let scaleRatio: CGFloat = self.cameraView.frame.height / image!.size.height
            scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio)

            origin = CGPointMake(-(image!.size.width - image!.size.height) / 2.0, 0)
        }else {
            let scaleRatio: CGFloat = self.cameraView.frame.width / image!.size.width
            scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio)

            origin = CGPointMake(0, -(image!.size.height - image!.size.width) / 2.0)
        }

        let size: CGSize = CGSizeMake(self.cameraView.frame.width, self.cameraView.frame.height)

        if (UIScreen.mainScreen().respondsToSelector(Selector("scale")) && UIScreen.mainScreen().scale >= 2.0) {
            UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
        }else {
            UIGraphicsBeginImageContext(size)
        }

        let context = UIGraphicsGetCurrentContext()
        CGContextConcatCTM(context, scaleTransform)

        image!.drawAtPoint(origin)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        self.cameraView.hidden = true
        self.imageView.hidden = false
        self.imageView.image = newImage

                    print("newImage width \(newImage.size.width )")
                    print("newImage height \(newImage.size.height)")
    })
}

注意:如果您不使用此库,请不要打扰cameraManager实例。相关代码只是简单地拍摄了镜头中的图像,因此图像实例是ios Camera拍摄的图像,即AVFoundation的输出。

除了信息 - 但是,我建议您查看CameraManager窗格。它极大地简化了AVFoundationUsage。

答案 1 :(得分:0)

这是Swift 3的修改:

=IF(O48="","",WEEKNUM(O48)-WEEKNUM(LOOKUP(O48,DATE(YEAR(O48),{1,4,7,10},1)))+1)