1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How to slice image to tiles?

Discussion in 'iPhone/iPad Programming' started by mikezang, Jan 8, 2011.

  1. macrumors 6502a

    #1
    I want to slice image to same size tiles, what can I do? is it easy and good way to do it?
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    Create a series of new UIImage objects and render the correct portion of the main image into each one.
     
  3. macrumors 6502a

    #3
    Do I need to use CGImageRef or any CG class?
     
  4. macrumors 6502a

    (marc)

    #4
    Take a look at UIGraphicsBeginImageContext, UIGraphicsGetImageFromCurrentContext and UIImage's drawInRect: methods.
     
  5. Moderator

    robbieduncan

    Staff Member

    #5
    You don't have to but if you want to you could use CGImageCreateWithImageInRect which will basically create a single tile once you convert to/from a CGImageRef.
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    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...
     
  7. macrumors 6502a

    #7
    Thanks, I will try.
     
  8. macrumors 6502a

    (marc)

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

    #9
    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...
     
  10. Moderator

    robbieduncan

    Staff Member

    #10
    Did you tell it to startAnimating?
     
  11. macrumors 6502a

    #11
    I did it, but no animation.
     
  12. Moderator

    robbieduncan

    Staff Member

    #12
    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.
     
  13. macrumors 6502a

    #13
    I found a AnimatedGif class on Internet, do you think which way is better? UIWebView or AnimatedGif?
     
  14. Moderator

    robbieduncan

    Staff Member

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

    #15
    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;
    }
    
     
  16. macrumors 6502a

    #16
    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?
     
  17. macrumors 68000

    #17
    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.
     
  18. macrumors 68000

    #18
    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.
     
  19. macrumors 6502a

    #19
    Can you tell me which way you draw the tiles, I post new thread
    http://forums.macrumors.com/showthread.php?t=1076891
     

Share This Page