How to slice image to tiles?

Discussion in 'iOS Programming' started by mikezang, Jan 8, 2011.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #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 emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Create a series of new UIImage objects and render the correct portion of the main image into each one.
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Do I need to use CGImageRef or any CG class?
     
  4. macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #4
    Take a look at UIGraphicsBeginImageContext, UIGraphicsGetImageFromCurrentContext and UIImage's drawInRect: methods.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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 emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    Thanks, I will try.
     
  8. macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #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. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #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 emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Did you tell it to startAnimating?
     
  11. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    I did it, but no animation.
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #13
    I found a AnimatedGif class on Internet, do you think which way is better? UIWebView or AnimatedGif?
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #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. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #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. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #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