PDA

View Full Version : How to slice image to tiles?




mikezang
Jan 8, 2011, 07:52 AM
I want to slice image to same size tiles, what can I do? is it easy and good way to do it?



robbieduncan
Jan 8, 2011, 08:02 AM
Create a series of new UIImage objects and render the correct portion of the main image into each one.

mikezang
Jan 8, 2011, 08:49 AM
Create a series of new UIImage objects and render the correct portion of the main image into each one.
Do I need to use CGImageRef or any CG class?

(marc)
Jan 8, 2011, 09:01 AM
Take a look at UIGraphicsBeginImageContext, UIGraphicsGetImageFromCurrentContext and UIImage's drawInRect: methods.

robbieduncan
Jan 8, 2011, 09:03 AM
You don't have to but if you want to you could use CGImageCreateWithImageInRect (http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGImage/Reference/reference.html#//apple_ref/c/func/CGImageCreateWithImageInRect) which will basically create a single tile once you convert to/from a CGImageRef.

robbieduncan
Jan 8, 2011, 09:23 AM
Take a look at UIGraphicsBeginImageContext, UIGraphicsGetImageFromCurrentContext and UIImage's drawInRect: methods.

I don't think that'll work. The draw in rect documentation says

Draws the entire image in the specified rectangle, scaling it as needed to fit

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...

mikezang
Jan 8, 2011, 10:26 AM
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...
Thanks, I will try.

(marc)
Jan 8, 2011, 11:40 AM
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...

The image's scale depends on the CGSize of the CGRect passed to the drawRect: method. Basically, drawAtPoint: is a special case of drawInRect:.

mikezang
Jan 8, 2011, 11:58 AM
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...

robbieduncan
Jan 8, 2011, 12:31 PM
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...

Did you tell it to startAnimating (http://developer.apple.com/library/ios/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImageView/startAnimating)?

mikezang
Jan 8, 2011, 12:40 PM
Did you tell it to startAnimating (http://developer.apple.com/library/ios/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImageView/startAnimating)?
I did it, but no animation.

robbieduncan
Jan 8, 2011, 12:43 PM
I did it, but no animation.

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.

mikezang
Jan 8, 2011, 12:52 PM
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?

robbieduncan
Jan 8, 2011, 01:04 PM
I found a AnimatedGif class on Internet, do you think which way is better? UIWebView or AnimatedGif?

I have no idea. Personally I'd use UIWebView as then it's up to Apple to fix bugs/improve performance.

xStep
Jan 8, 2011, 10:03 PM
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.

- (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 = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

aUIImage = [UIImage imageWithCGImage: tempSubImage];
[cellArray addObject: aUIImage];

aUIImage = nil;
CGImageRelease(tempSubImage);

}
}

return cellArray;
}

mikezang
Jan 9, 2011, 03:18 AM
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.

- (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 = CGImageCreateWithImageInRect(masterImage.CGImage, tempRect);

aUIImage = [UIImage imageWithCGImage: tempSubImage];
[cellArray addObject: aUIImage];

aUIImage = nil;
CGImageRelease(tempSubImage);

}
}

return cellArray;
}

Thanks, I will try to test it.

By the way, I am not sure if I have to post a new thread, I want to know if it is possible to slice a video? how about performance if show 16 tiles video if it is possible?

xStep
Jan 9, 2011, 06:05 AM
I'm just starting to look into video. Perhaps the AVFoundation stuff will work. It looks like AVCaptureVideoDataOutput may be your in for processing the frames.

xStep
Jan 9, 2011, 03:22 PM
Forgot 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.

mikezang
Jan 9, 2011, 05:43 PM
Forgot 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.
Can you tell me which way you draw the tiles, I post new thread
http://forums.macrumors.com/showthread.php?t=1076891