iOS如何从UIView中保存图像而不会丢失图像质量

时间:2015-09-16 13:02:44

标签: ios iphone image uiview uiimage

我有在UIView中绘制的图像,然后在它们上绘制线条。如何使用绘制的线条将图像保存到具有相同图像尺寸的UIImage对象,并且不会丢失任何图像质量?

即。我有一个大小的图像(3264,2448)。 这是在UIView(大小375,281)中绘制的AspectFit与图像, 然后在图像上画一条线。最后,如何在不损失图像质量的情况下将图像从UIView保存到尺寸为(3264,2448)的UIImage?

如果这不是最好的方法,请推荐一种更好的方法来实现这一目标。

class DrawingView: UIImageView {

    private var pts = [CGPoint](count: 5, repeatedValue: CGPoint())
    private var ctr: uint!

    var lineWidth: CGFloat = 4.0
    var lineColor: UIColor = UIColor.darkGrayColor()

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

    init(frame: CGRect, image: UIImage) {
        super.init(frame: frame)
        self.image = image
        beginDrawingView()
   }

   private func beginDrawingView() {
        userInteractionEnabled = true
   }

   override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        ctr = 0
        if let touch = touches.first as? UITouch {
            pts[0] = touch.locationInView(self) as CGPoint
        }
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            let p: CGPoint = touch.locationInView(self)
            ctr = ctr + 1
            pts[Int(ctr)] = p
            if ctr == 4 {
                pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); 

                UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
                let context = UIGraphicsGetCurrentContext()

                image!.drawInRect(CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height))
                CGContextSaveGState(context)

                CGContextSetShouldAntialias(context, true)
                CGContextSetLineCap(context, kCGLineCapRound)
                CGContextSetLineWidth(context, lineWidth)
                CGContextSetStrokeColorWithColor(context, lineColor.CGColor)

                let path = CGPathCreateMutable()
                CGPathMoveToPoint(path, nil, pts[0].x, pts[0].y)
                CGPathAddCurveToPoint(path, nil, pts[1].x, pts[1].y, pts[2].x, pts[2].y, pts[3].x, pts[3].y)

                CGContextSetBlendMode(context, kCGBlendModeNormal)

                CGContextAddPath(context, path)
                CGContextStrokePath(context)


                image = UIGraphicsGetImageFromCurrentImageContext()
                CGContextRestoreGState(context)
                UIGraphicsEndImageContext()

                pts[0] = pts[3]
                pts[1] = pts[4]
                ctr = 1
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我不确定这是否正是您所寻找的,但如果您已有UIImage个对象,则可以将其另存为Base64String。以下是我使用的一些代码(您会注意到我也对任何JPG图像执行压缩,因为我将字符串保存到数据库中,但当然您不需要该组件因为你不想丢失图像质量):

//Convert the image to Base64String with optional JPG compression
//
-(NSString*)convertImageToBase64String:(UIImage*)image withImageType:(NSString*)imageType
{
    NSData* data;
    if ([imageType isEqualToString:@"PNG"]) {
        data = UIImagePNGRepresentation(image);
    } else if ([imageType isEqualToString:@"JPG"] || [imageType isEqualToString:@"JPEG"]) {
        data = UIImageJPEGRepresentation(image, 0.9);
    }

return [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

如果你还没有UIImage对象,你可以通过这样的截图得到它:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return img;
}

我还没有对最后一段代码进行测试,但我怀疑它会为您提供整个屏幕的屏幕截图,因此您需要使用view.bounds.size组件来获取您正在寻找的确切区域对于。希望有所帮助!