UIImageView裁剪

时间:2015-05-27 13:52:41

标签: swift uiimageview crop

我正在尝试允许用户裁剪图像。问题是裁剪的图像与视图不同。这是两个截图,以显示我的意思。

预裁片屏幕:

http://i58.tinypic.com/4rap9u.png

裁剪后

http://i58.tinypic.com/2nrpjlj.png

这是我用来创建新裁剪图像的代码。 UIImageView位于UIScrollView内,允许用户缩放和平移。即使图像没有被缩放,它也无法正常工作。理想情况下,我希望允许用户在屏幕周围移动虚线方框,以选择要裁剪的图像部分,以及放大/缩小。

func croppedImage() -> UIImage?
{
    var drawRect: CGRect = CGRectZero
    var cropRect : CGRect!

    cropRect = photoFrameView?.frame

    zoom = imgScroll.zoomScale
    println(zoom)

        drawRect.size = imgPreview.bounds.size
        drawRect.origin.x = round(-cropRect.origin.x * zoom)
        drawRect.origin.y = round(-cropRect.origin.y * zoom)
        cropRect.size.width = round(cropRect.size.width*zoom)
        cropRect.size.height = round(cropRect.size.height*zoom)
        cropRect.origin.x = round(cropRect.origin.x)
        cropRect.origin.y = round(cropRect.origin.y)

        UIGraphicsBeginImageContextWithOptions(cropRect.size, false, UIScreen.mainScreen().scale)

        self.imgPreview.image?.drawInRect(drawRect)

        var result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        return result
}

这是裁剪图像的地方:

    let cImg = croppedImage()!
    UIImageWriteToSavedPhotosAlbum(cImg, nil, nil, nil);

    imgPreview.image = cImg
    self.imgPreview.contentMode = .ScaleAspectFit

此处还有我的UIView子类(photoFrameView)的代码。不确定是否需要。

import UIKit

class mycropView: UIView {

var lastLocation:CGPoint = CGPointMake(0, 0)

override init(frame: CGRect) {
    super.init(frame: frame)
    // Initialization code
    var panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
    self.gestureRecognizers = [panRecognizer]

    self.addDashedBorder()
    self.backgroundColor = UIColor.clearColor()
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func detectPan(recognizer:UIPanGestureRecognizer) {

    var translation  = recognizer.translationInView(self.superview!)

    self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    // Promote the touched view
    self.superview?.bringSubviewToFront(self)

    // Remember original location
    lastLocation = self.center
}

override func hitTest(point: CGPoint, withEvent e: UIEvent?) -> UIView? {

    if let result = super.hitTest(point, withEvent:e) {
        println("hit inside")
        return result
    } else {
        self.removeFromSuperview()
    }
    return nil
}

}

extension UIView {
func addDashedBorder() {
    let color = UIColor.yellowColor().CGColor

    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let frameSize = self.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clearColor().CGColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.lineDashPattern = [6,3]
    shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).CGPath

    self.layer.addSublayer(shapeLayer)

}
}

0 个答案:

没有答案