gpt4 book ai didi

ios - 如何保存图像和其上绘制的线条的组合? swift

转载 作者:行者123 更新时间:2023-11-30 13:32:09 25 4
gpt4 key购买 nike

我已经保存了图像,但是每当我在图像上绘制线条并尝试保存图像时,图像不会与我在其上绘制的线条一起保存。如何保存画有线条的图像?

这是当前绘制线条的代码:

import UIKit
import CoreData

class DrawClass: UIView {
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
///Creating an array of type Line which accepts a Line object. It is an empty array.
var lines:[Line] = []
var lastPoint: CGPoint!

required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.backgroundColor = UIColor.whiteColor()
self.layer.zPosition = 1
}

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

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent){
if let touch = touches.first as? UITouch {
var newPoint = touch.locationInView(self)
lines.append(Line(start: lastPoint, end: newPoint))
lastPoint = newPoint
}
self.setNeedsDisplay()
}

override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextBeginPath(context)
for line in lines
{
CGContextMoveToPoint(context, line.start.x, line.start.y)
CGContextAddLineToPoint(context, line.end.x, line.end.y)
}

var storage: Float = ViewController.simple.storage1
var storage2: Float = ViewController.simple.storage2
var storage3: Float = ViewController.simple.storage3

CGContextSetRGBStrokeColor(context, CGFloat(storage), CGFloat(storage2), CGFloat(storage3), 1)

CGContextSetLineWidth(context, 5)
CGContextStrokePath(context)
}
}

更新抱歉,我忘记添加保存方法了。顺便说一句,“地球”是我放置在 Storyboard中的图像的名称,它是:

@IBAction func saveImage(sender: UIButton) {


UIImageWriteToSavedPhotosAlbum(earth.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)

}

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else {
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}

最佳答案

更改当前的 saveImage 方法实现。

@IBAction func saveImage(sender: UIButton) {


UIImageWriteToSavedPhotosAlbum(earth.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)

}

这一切都是为了显式保存 UIImageView 实例 earth 的图像。

要将它们与线条一起保存,您需要从地球 View 创建一个新图像,如下所示:

     @IBAction func saveImage(sender: UIButton) {

UIGraphicsBeginImageContextWithOptions(earth.frame.size, earth.opaque, 0.0)
earth.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let imageWithLines = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

// NOW you have the image to save
UIImageWriteToSavedPhotosAlbum(imageWithLines, self, Selector("saveComplete"), nil)
}

func saveComplete () {
NSLog("Complete!");
}

关于ios - 如何保存图像和其上绘制的线条的组合? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com