基于点阵图来构建 Bezier 路径
我现在正在努力地编写那本关于 UIKit / Quartz 的书,书中描述了很多使用 Bezier 路径绘图的案例。今天,在进行了一天忙碌的写作之后,我现在决定好好休息、放松一下。 因此我登上了 IRC (Internet Relay Chat),在那里我遇到了一个很有意思的挑战。它是以 Clarus the dog cow 的形式出现的(译者注:一只像牛的狗,这个卡通形象由苹果传奇图形设计师 SusanKare 设计,在早期的 Mac 系统中,用来显示打印页面的朝向)。这只狗狗是以点阵图 (bitmap) 的形式出现的,通常情况下将其转换为 而今,受到我对 PaintCode 的评论的启示,我决定创建一个通用的「点阵图→Bezier 路径」方法来解决这个问题。我最终将会得到一个扩展类 (Category),能够将字节转换为易于绘制的 objective-c @implementation UIBezierPath (BitmapExtension) + (UIBezierPath *) pathFromBitmap: (NSData *) data size: (CGSize) size { Byte *bytes = (Byte *) data.bytes; CFIndex height = size.height; CFIndex width = size.width; if (height * width != data.length) { NSLog(@"Error: size does not match data's available bytes"); return nil; } UIBezierPath *bezierPath = [UIBezierPath bezierPath]; for (CFIndex y = 0; y < height; y++) { for (CFIndex x = 0; x < width; x++) { CFIndex index = y * width + x; if (bytes[index] != 0) { UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x,y,1,1)]; [bezierPath appendPath:path]; } } } return bezierPath; }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |