I need to scroll a background image larger than the screen size and make it loop endlessly
I am writing an ipad app that I hav开发者_StackOverflowe locked the orientation to landscape mode for. I want to have a PNG file that is 100 pixels high but 4 times the width of the ipad screen.
What I want is this background image to be shown on the screen then automatically begin scrolling on a timer. I have made sure the start and end parts of this image match up. Once the end of the image is reached, I would like the start bit to show again. This image should scroll along either at one pixel movements or larger jumps if that is too slow.
I am really stuck looking for some example code that would show how to show to do this.
It is NOT a game I am writing so do not want to use OpenGL or anything fancy.
The examples I have looked at so far are not exactly what I am after.
Can I simplify and state exactly what it is I am stuck on..
Lets say I load an image into memory that is 1000 pixels wide by 100 in height.
Can someone show me a few lines of code that would let me cut out a rectangle that begins say 200 pixels across the x axis into this larger image and is a rectangle itself 100 by 100 pixels.
How could I cut this rectangle out and show it on the screen - speed is not important as stated before.
I can then work out the scrolling part from this myself (I hope) and hopefully post it back here later..
I am thinking along the lines of the CGContext type commands but despite looking at examples and at docs am still up against a brick wall. It is this 'cutting a rectangular section out' that I am stumbling most at.
Thanks.
Any help appreciated.
Disclosure - I'm not an iPhone dev at all.
But, what I would do is break the image into chunks. (Maybe 8) So, you would end up having 8 chunks each 100px tall, and 1/2 the width of the screen.
Then, as the image scrolls, you can load each image before its turn to be displayed. So, as images flow off the screen, you can replace them with the next image to be displayed on the other side of the screen.
Disclosure - I'm not an iPhone dev at all. (Borrowed that line from jjnguy)
Take your background image and expand it by one screen's worth, and copy the beginning of the screen to the new area. Now all you have to do is crop the desired part and display it - the wraparound has already been taken care of.
Thanks for all your help and advice.
In the end I decided the simplest way to do it was what Mark Ransom had suggested - as that takes care of the wraparound.
I used the 4 lines of code below to cut out a section I wanted 200 pixels wide by 100 high.
I then looped this varying startX on a timer. Perfect!
// Our image rectangle is 200 pixels wide by 100 pixels high
CGRect selectionRect = CGRectMake(startX, 0.0, 200.0, 100.0);
CGImageRef resultImageRef = CGImageCreateWithImageInRect(masterImage.CGImage, selectionRect);
UIImage *outputFrame = [[UIImage alloc] initWithCGImage:resultImageRef];
[outputImageViewOutlet setImage:outputFrame];
In the end.. it is CGRectMake and CGImageCreateWithImageInRect anyone wanting to do this should look further into!
Thanks all. Hope this helps someone.
精彩评论