在UIImage中按顺序移动方块



我是Objective-C的新手,但我需要编写一个快速的方法,它将UIImage划分为固定大小的方块,然后将它们混合在一起。我已经通过以下方式实现了它:

  1. 获取UII法师
  2. 将其表示为 PNG
  3. 将其转换为 RGBA8 无符号字符数组
  4. 对于每个块,
  5. 计算其坐标,然后用替换的块中的像素对每个像素进行 xor
  6. 将RGBA8肉组装回新的UIImage中
  7. 归还它

它按预期工作,但速度非常慢。在 iPhone 4S 上处理单个 1024x768 PNG 大约需要 12 秒。检查器显示,以某种方式连接到PNGRepresentation的方法占用了总运行时间的50%左右。

如果我以某种方式在这里使用 Quartz2D,它会更快吗?我现在只是尝试从我的_image复制/粘贴单个矩形,但我不知道如何进一步。它返回一个 UIImage,其中按原样提供_image,其中不粘贴块层:

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;

您可以按照以下步骤执行所需的操作:

  • 加载图像
  • 如何将其分成正方形
  • 为每个图像创建一个CALayer,在随机播放之前将位置设置为图像中正方形的位置
  • 遍历图层,并在随机播放后将其位置设置为目标位置
  • 看着方块移动到新的地方,如果你不想要动画怎么办?

最新更新