views:

57

answers:

1

Hi,

I'm working on an iPhone app which needs to have the function to reveal an image after some actions.

For example:

I have 2 UIImages on top of each other. The UIImage on the back is the image to reveal. The UIImage on top needs to cover this image. I need a method that I can tell to reveal the image. So if I call the method, and let's say I give as a parameter 50 (which stands for 50%) the half of the pixels of the top UIImage need to be set to opacity 0. I know that I have to read the image into data, and then I can edit pixels. But I need to edit random pixels, otherwise if I call the method with 50 the top of the image is already visible. So I need to set the opacity of the pixels to 0, but random. This will give you a kind of "snow" effect, and for example when I call the method and give 100 as parameter this means that 100% of the pixels have to get the opacity of 0, so the complete picture is visible.

The random is important, because using random, this reveals the image slowly instead of revealing half of the image because than the fun is over ;-)

To make it easy: The image to edit the pixels from is ALWAYS 320x480. I only use landscape!

I hope somebody can help me, that would be great!

Thanks in advance!

A: 

Check out the answer to this question. That will tell you how to edit the Image's pixel data. The rest is just logic.

I'd use an array like NSArray *alphaTable which has a capacity of 320*480 (or inputs from the image size).

Then I'd iterate over that array, pseudo-code:

static int totalRandomCount = 0;
for (int i = 0; i< [alphaTable count]; i++) {

   if ( rand() > inputPercent && [alphaTable objectAtIndex:i] > 0.0f) {
       [alphaTable setValueAtIndex: 0.0f];
       totalRandomCount++;
   }
   if (totalRandomCount > [alphaTable count] * inputPercent) return;

}

That should make previous edits persistent. (i.e., first pass inputPercent is 10%, then 25%, then 50%, then 90%, then 100% should give you a continuously appearing image without changing which pixels were previously displayed.)

It's not an exact method, so it shouldn't be used for anything but a "fun" app.

Stephen Furlani
Dear Stephen, Thank you for your quick reply. The link you gave me I already found and implemented. But the problem is t takes a long time to generate the arrays. For example, I first create an array with valid numbers with:for(int i = 0, i < 156000, i += 4)add i to the arrayThis gives me the valid numbers for random. After that, I'm creating an array with random numbers. To avoid double numbers, I have to check for every random number if it's already in the array. All this takes a long time. Sometime even 10 minutes so this makes the app very slow. So I want to know how I can speed it up
CyberK
hoooookay, now I'm confused. The array should only contain the alpha values (no need for i+=4) and have only values 1 or 0 depending on whether or not that pixel is revealed or not. Otherwise, I have no idea what you're asking for. Why are you generating an array with random numbers?
Stephen Furlani