ios – 我可以为折线绘制缩略图吗?

ios – 我可以为折线绘制缩略图吗?,第1张

概述如果我有一条折线保存为字符串的路径, 来自谷歌地图sdk:path.encodedPath() 或者我有一系列的latlngs积分, 我可以为该路径绘制缩略图吗? 我不想在mapView上绘制它,我想知道我是否可以在任何其他视图中绘制它,如imageView或任何类似的东西. 我创建了 swift 3代码,您可以在 *** 场上复制和粘贴,并立即查看结果 代码在这里: import UIKitimpor 如果我有一条折线保存为字符串的路径,

来自谷歌地图sdk:path.encodedpath()

或者我有一系列的latlngs积分,

我可以为该路径绘制缩略图吗?

我不想在mapVIEw上绘制它,我想知道我是否可以在任何其他视图中绘制它,如imageVIEw或任何类似的东西.

解决方法 我创建了 swift 3代码,您可以在 *** 场上复制和粘贴,并立即查看结果

代码在这里:

import UIKitimport PlaygroundSupportvar str = "Hello,playground"//All you need is to create a path with that points and create image or layer with that path//To perpare for this let make some extensions with helper code//Extension for UIBezIEPath to easily create it from pointsextension UIBezIErPath{    convenIEnce init(points:[CGPoint])    {        self.init()        //connect every points by line.        //the first point is start point        for (index,aPoint) in points.enumerated()        {            if index == 0 {                self.move(to: aPoint)            }            else {                self.addline(to: aPoint)            }        }    }}//to create image from path you can use this class functionextension UIImage{    class func imageFrom(path:UIBezIErPath,linecolor:UIcolor,fillcolor:UIcolor)->UIImage    {        //create context to draw in use path bounds as context size. assume that path is inzIDe of rect with start corener at 0,0 coordinate        UIGraphicsBeginImageContextWithOptions(path.bounds.size,false,0)        print("path bounds \(path.bounds) linewidth:\(path.linewidth)")        let context = UIGraphicsGetCurrentContext()        //set fill color        context?.setFillcolor(fillcolor.cgcolor)        //set line coolor        context?.setstrokecolor(linecolor.cgcolor)        context?.setlinewidth(path.linewidth)        //draw a path        context?.addpath(path.cgPath)        context?.drawPath(using: .fillstroke)        //get image from context        let image = UIGraphicsGetimageFromCurrentimageContext()!        //finish context        UIGraphicsEndImageContext()        return image    }}//2. To create layer use this extensionextension CAShapeLayer{    convenIEnce init(path:UIBezIErPath,fillcolor:UIcolor)    {        self.init()        self.path = path.cgPath        self.strokecolor = linecolor.cgcolor        self.fillcolor = fillcolor.cgcolor        self.linewidth = path.linewidth        self.opacity = 1        self.frame = path.bounds    }}//how to use://1. assume you recIEved pointslet points:[CGPoint] = [CGPoint(x: 0,y: 0),CGPoint(x: 150,y: 50),CGPoint(x: 75,y:140),CGPoint(x: 0,y: 80)]//2. create pathlet path = UIBezIErPath(points: points)//3. you can specify path line wIDthpath.linewidth = 2//4. as a joinstyle toopath.lineJoinStyle = .round//5. a)Now you can create image from path with helper functionlet image = UIImage.imageFrom(path: path,linecolor: UIcolor.purple,fillcolor: UIcolor.red)print(image)//and set it to imageVIEwlet imageVIEw = UIImageVIEw(image: image)imageVIEw.frame.origin = CGPoint(x: 200,y: 200)imageVIEw.backgroundcolor = UIcolor.green//5. Maybe you will need to specify content mode for imageVIEwimageVIEw.contentMode = .scaleAspectFit//5 b.) Or you can create a Layer. Add add it to someone's layer layter//if you need,you can apply transform to path - this is special way to //adjust scale,rotation an lots of other cool stuff on layers,paths.//Create special struct which descripbes transformation//IDentity is a special case which does not make any transformations at allvar transform = CGAffinetransform.IDentity//scale it by 0.5 for x and 0.5 for y. if you need to increse scale by//100 times,just pass 100 for x and y argumentstransform = transform.scaledBy(x:  0.5,y: 0.5)//no apply transform to path.path.apply(transform)let layer = CAShapeLayer(path: path,linecolor: UIcolor.blue,fillcolor: UIcolor.brown)//6. let see resultslet container = UIVIEw(frame: CGRect(x: 0,y: 0,wIDth: 400,height: 400))container.backgroundcolor = UIcolor.white//for imageVIEwcontainer.addSubvIEw(imageVIEw)//for CAShapeLayercontainer.layer.addSublayer(layer)//for playGround you can set this to see result there//Do not forget to select from menu//VIEw -> Assistant Editor-> Show Assistance EditorPlaygroundPage.current.liveVIEw = containerPlaygroundPage.current.needsIndefiniteExecution = true

//我还要提一下,CAShapeLayer解决方案占用的内存较少,这对于真正的大图像至关重要
//但是UIImage更容易使用

棕色图形是路径缩放0.5的图层,红色图形是imageVIEw

总结

以上是内存溢出为你收集整理的ios – 我可以为折线绘制缩略图吗?全部内容,希望文章能够帮你解决ios – 我可以为折线绘制缩略图吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1070773.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存