在 Swift 中从 Uibezierpath 数组创建一个 UIImage [英] Create an UIImage from Uibezierpath Array in Swift
本文介绍了在 Swift 中从 Uibezierpath 数组创建一个 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Swift 中从多个 UIBezierPaths 的数组创建一个 UIImage.
I want to create an UIImage from an Array of multiple UIBezierPaths in Swift.
我尝试了以下代码:
func convertPathsToImage(paths: [UIBezierPath]) -> UIImage{
let imageWidth: CGFloat = 200
let imageHeight: CGFloat = 200
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()!
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(nil, Int(imageWidth), Int(imageHeight), 8, 0, colorSpace, bitmapInfo.rawValue)
CGContextBeginPath(context);
UIColor.blackColor().set()
for path in paths{
path.stroke()
}
let newImageRef = CGBitmapContextCreateImage(context);
let newImage = UIImage(CGImage: newImageRef!)
return newImage
}
结果是一个空图像.
谁能解释一下我做错了什么?非常感谢!
Can someone explain me what i`m doing wrong? Thank you very much!
推荐答案
我不能告诉你你做错了什么,但我遇到了类似的问题,这段代码对我有用.
I can't tell you what you are doing wrong, but I had a similar issue, and this code works for me.
func convertPathsToImage(paths: [UIBezierPath]) -> UIImage
{
let imageWidth: CGFloat = 200
let imageHeight: CGFloat = 200
let strokeColor:UIColor = UIColor.blackColor()
// Make a graphics context
UIGraphicsBeginImageContextWithOptions(CGSize(width: imageWidth, height: imageHeight), false, 0.0)
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor)
for path in paths {
path.stroke()
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
这篇关于在 Swift 中从 Uibezierpath 数组创建一个 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文