I need to split an UIImage in 9 buttons. Now I have nine buttons exactly occupying all the iPad screen, but I want to crop the big image in nine spaces, specifically on the buttons size/position.
Now I have this code:
CGRect cropRect = CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.height);
CGImageRef croppedImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
[button setImage:image forState:UIControlStateNormal];
CGImageRelease(croppedImage);
But, all the buttons are showing the same image.
Anyone have an idea of what is happening and how to solve this problem (Cropping the image in nine pieces, in the X, Y, Width and Weight of the button)?
I'm really new in this.
--EDIT--
I just changed:
[button setImage:[UIImage imageWithCGImage:croppedImage] forState:UIControlStateNormal];
Thanks, Martin Pilch! :-)
You are setting the original image to the buttons. Try to set it this way:
[button setImage:[UIImage imageWithCGImage:croppedImage] forState:UIControlStateNormal];
Hope it helps