Note that there are some explanatory texts on larger screens.

plurals
  1. POSequentially shift square blocks in UIImage
    text
    copied!<p>I am new to Objective-C, but I need to write a fast method, which will divide an UIImage into square blocks of fixed size, and then mix them. I have already implemented it in the following way:</p> <ol> <li>Get UIImage</li> <li>Represent it as PNG</li> <li>Convert it to RGBA8 unsigned char array</li> <li>For each block, calculate it's coordinates, then xor each pixel with pixel from block that gets replaced</li> <li>Assemble that RGBA8 meat back into a new UIImage</li> <li>Return it</li> </ol> <p>It works as intended, but it is extremely slow. It takes about 12 seconds to process single 1024x768 PNG on iPhone 4S. Inspector shows that methods somehow connected to PNGRepresentation, eat up about 50% of total run time.</p> <p>Will it possibly be faster, if I use Quartz2D here somehow? I am now simply trying to copy/paste a single rectangle from and to my _image, but I don't know how to go further. It returns an UIImage with the _image provided as is, without the blockLayer pasted inside it:</p> <pre><code>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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload