【发布时间】:2012-07-03 18:18:01
【问题描述】:
我是Objective-C的新手,但是我需要写一个快速的方法,它将一个UIImage分成固定大小的正方形块,然后混合它们。我已经通过以下方式实现了它:
- 获取 UIImage
- 将其表示为 PNG
- 将其转换为 RGBA8 无符号字符数组
- 对于每个块,计算其坐标,然后将每个像素与被替换的块中的像素进行异或操作
- 将 RGBA8 肉重新组装到新的 UIImage 中
- 退货
它按预期工作,但速度极慢。在 iPhone 4S 上处理单个 1024x768 PNG 大约需要 12 秒。 Inspector 显示以某种方式连接到 PNGRepresentation 的方法会占用大约 50% 的总运行时间。
如果我在这里以某种方式使用 Quartz2D,它可能会更快吗?我现在只是试图从我的_image 复制/粘贴一个矩形,但我不知道如何更进一步。它返回一个 UIImage 并按原样提供 _image,其中没有粘贴 blockLayer:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
/* Start drawing */
//Draw in my image first
[_image drawAtPoint:CGPointMake(0,0) blendMode:kCGBlendModeNormal alpha:1.0];
//Here I am trying to make a 400x400 square, starting presumably at the origin
CGLayerRef blockLayer = CGLayerCreateWithContext(context, CGSizeMake(400, 400), NULL);
//Then I attempt to draw it back at the middle
CGContextDrawLayerAtPoint(context, CGPointMake(1024/2, 768/2), blockLayer);
CGContextSaveGState(context);
/* End drawing */
//Make UIImage from context
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
【问题讨论】:
标签: objective-c ios uiimage quartz-2d cgcontextref