Do I need to use CGImageRef or any CG class?Create a series of new UIImage objects and render the correct portion of the main image into each one.
Take a look at UIGraphicsBeginImageContext, UIGraphicsGetImageFromCurrentContext and UIImage's drawInRect: methods.
Draws the entire image in the specified rectangle, scaling it as needed to fit
Thanks, I will try.I don't think that'll work. The draw in rect documentation says
So you'd end up with loads of small copies of the entire image instead of the image chopped into tiles. drawAtPoint would work, I think, as it does not scale the image. That said the CG function I linked to seems easier...
I don't think that'll work. The draw in rect documentation says
So you'd end up with loads of small copies of the entire image instead of the image chopped into tiles. drawAtPoint would work, I think, as it does not scale the image. That said the CG function I linked to seems easier...
By the way, is it possible to show a animated GIF file in UIImageView? I used the same code to load gif89a file, but it seems like no animation...
I did it, but no animation.
I found a AnimatedGif class on Internet, do you think which way is better? UIWebView or AnimatedGif?You may well have to use a UIWebView. UIImageView seems to expect an array of images to animate with. UIImage does not seem to support the concept of multiple animation frames within an image so it's likely that what you are trying to do simply doesn't work.
I found a AnimatedGif class on Internet, do you think which way is better? UIWebView or AnimatedGif?
- (NSMutableArray *) setUpCellsUsingImage: (UIImage *) masterImage
{
NSMutableArray * cellArray;
NSInteger row, col;
CGImageRef tempSubImage;
CGRect tempRect;
CGFloat yPos, xPos;
UIImage * aUIImage;
cellArray = [[NSMutableArray new] autorelease];
for (row=0; row < rows; row++) {
yPos = row * containerCellHeight;
for (col=0; col < cols; col++) {
xPos = col * containerCellWidth;
tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight);
tempSubImage = [COLOR="Red"]CGImageCreateWithImageInRect[/COLOR](masterImage.CGImage, tempRect);
aUIImage = [UIImage imageWithCGImage: tempSubImage];
[cellArray addObject: aUIImage];
aUIImage = nil;
CGImageRelease(tempSubImage);
}
}
return cellArray;
}
Thanks, I will try to test it.I pulled this from some code I've been working on. It will split an image into individual tiles and return those in an array.
In this object I have some instance variables you'll have to make you own assumptions for; rows, cols, containerCellHeight, containerCellWidth. Those are decisions made before this code is called.
I used CGImageCreateWithImageInRect to get my tiles.
Code:- (NSMutableArray *) setUpCellsUsingImage: (UIImage *) masterImage { NSMutableArray * cellArray; NSInteger row, col; CGImageRef tempSubImage; CGRect tempRect; CGFloat yPos, xPos; UIImage * aUIImage; cellArray = [[NSMutableArray new] autorelease]; for (row=0; row < rows; row++) { yPos = row * containerCellHeight; for (col=0; col < cols; col++) { xPos = col * containerCellWidth; tempRect = CGRectMake(xPos, yPos, containerCellWidth, containerCellHeight); tempSubImage = [COLOR="Red"]CGImageCreateWithImageInRect[/COLOR](masterImage.CGImage, tempRect); aUIImage = [UIImage imageWithCGImage: tempSubImage]; [cellArray addObject: aUIImage]; aUIImage = nil; CGImageRelease(tempSubImage); } } return cellArray; }
Can you tell me which way you draw the tiles, I post new threadForgot to mention a couple of things regarding the tile split code.
The image I'm passing has been resized so that the split aligns with pixels, not on fractions. I found this easier to fix a problem when drawing the tiles onto my the underlying view. I'd get odd grid lines on a device, but not the emulator.
If I recall right, UIImage and CGImage have different origins. So in my case, reconstructing the tiles into a view got me tiles out of vertical order. That is just something to watch out for.