Image stretching - iphone

I want Stretch a image. For that i use sprite. I want stretch sprite & this stretching is may be Circular or curve animation. I don't understand what methode used for that. Can anyone help me?

Since you tagged your question with cocos2d I guess you'll be using that. It's really basic to strech an image
Sprite *mySprite = [Sprite spriteWithFile:#"mysprite.png"];
mySprite.position = ccp(100, 100);
mySprite.scale = 2.0;
[self addChild:mySprite];
If you want to animate it you can use the cocos2d actions or just create your own animation. The example below does a linear animation to 3x original sprite size in 1 second:
id action1 = [ScaleTo actionWithDuration:1.0 scale:3.0];
[mySprite runAction: action1];

For manipulating views and images in general in ways such as streching you can read up on transforms provided by the sdk, you can learn about 2D transforms here http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html and you can extend that further to 3D by manipulating the layers transforms instead of the views transforms. Youll be able to do things such as scaling and rotating and you can define your own transforms as well. This example project http://developer.apple.com/iphone/library/samplecode/MoveMe/ is a good reference to get started with transforms and animating them.

Related

How to flip a CGImageRef over y-axis

I'm trying to animate a sprite using only CoreAnimation. It's working so far except I can't figure out how to flip the sprite sheet.
Right now, I have a walking animation, but I want the sprite to face the direction it's walking (because it looks kind of silly walking backwards).
I guess I could add the reversed images on the sprite sheet, but I would rather avoid that because it could make it really big when I decide to add more.
Right now, my sprite is extending CALayer and I've set its contents to the CGImageRef which is the sprite sheet:
self.contents = (id) image;
To flip it I tried:
UIImage *tmp = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUpMirrored];
CGImageRef flippedImage = tmp.CGImage;
self.contents = (id) flippedImage;
..and that's not working.
I found other solutions which involve animating, but I don't want to animate the flip. I just want it to happen instantly.
Is there a simple way to do this?
If there's a way to flip the whole CALayer, I'd like to know that too. =]
Thanks!
Rather than flipping your image, you could try applying a transform to your layer object.
Something like:
self.transform = CATransform3DMakeScale(-1, 1, 1);
This may be better for performance too; if the sprite needs to walk in the opposite direction, you can just set your transform back to CATransform3DIdentity rather than allocing a new image and rotating it.

Tiling an image that is part of a texture atlas

I'm using Cocos2D. What is the most efficient way to tile an image when it's part of a texture atlas that's been generated using Texture Packer. I have an image that is 10 x 320 and I want to tile it to fill the screen.
I've used this code before for tiling images
bgHolder = [CCSprite spriteWithFile:#"bg.png" rect:CGRectMake(0, 0, 700, 300*155)];
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[bgHolder.texture setTexParameters:&params];
[self addChild:bgHolder];
but I don't think I can use this approach when the image I want to tile isn't square and is only a small part of the over al texture.
Chaining a bunch of CCSprites seems pretty inefficient to me so I'm hoping there is a better way.
Use one sprite per tile. That's the way to do it. You should use sprite batching to keep the number of draw calls to 1. Rendering 48 sprites is not much worse than rendering one 480x320 sprite when using sprite batching.

Animated Masks in C4

I know that it is possible to create layer masks in C4 like this:
object.layer.mask = anotherObject.layer;
Is there a known way to use an animated mask?
Yes. You can animate a mask in a couple of different ways.
First, if you use basic shapes as the object whose layer will become the mask, you can animate them as a you would normally and this becomes an animated mask.
This can be done for any visible object in C4 (i.e. shapes, movies, images, etc...).
For instance:
object.layer.mask = aShape.layer;
aShape.animationDuration = 1.0f;
aShape.origin = CGPointMake(x, y);
The above can be done with images as well. When using images any clear parts of the image will turn out transparent in your original object.
Furthermore, there is an undocumented animatable image method, which is experimental and available only in the latest template.
Using it would look like:
NSArray *imageNamesArray = [NSArray arrayWithObjects:#"imageName01.png",...,nil];
C4Image *animatedImage = [C4Image animatedImageWithNames:imageNamesArray];
object.layer.mask = animatedImage.mask;
Essentially, this method creates an animated gif style image... But, because this method is brand new / experimental, there isn't any control over the speed of the transitions between images.

how to apply an imageview frame with the inclined coordinates

hi all upto now i know making rectangle with the CGrectmake and this rect(frame) i am using as imageview frame like UIImageView *someImage=[[uiimageview alloc]initwithframe:someRect]; now i can add an image with the frame of someRect. my problem here is when the coordinates like
(rectangleFirstx-coordinate,tectangleFirstY-cordinate)=(10,10)
(rectangleLastx-cordinate,rectangleLasty-cordinate)=(17,7) this, how can i give frame to the uiimageview....This is like a inclined rectangle..can any one suggest me how to apply frame through the ios library for these type of coordinates..Thanks in advance..
Your example isn't very clear because a rectangle with opposite corners at (10,10) and (10,7) can be in any one of a myriad of different orientations, including one perfectly aligned along the x and y axis.
What you can certainly do is create a UIImageView of the desired size and location and then rotate it by using one of many techniques, including animation methods.
[UIImageView animateWithDuration:0.1 animations:^
{
your_UIImageView_here.transform = CGAffineTransformMakeRotation((M_PI/180.0) * degrees);
}];
You can hide the UIImageView until the rotation is done and then show it.
If your question is about how to use the coordinates you provided to arrive at an angle I'd suggest that more data is needed because it is impossible to pick one of the billions of possible rectangles with corners at those two points without more information. Once you have more data then it is pretty basic trigonometry to figure out the angle to feed into the rotation.

rotating image in quartz 2d

i want to rotate image in quartz 2d around one corner..i want to make analog clock.
You should probably start with the Quartz 2D Programming Guide. In particular, check out the section on transforms, this covers rotation.
Essentially what you need to do is apply a transform matrix for rotation to the Current Transform Matrix (CTM). This defines the mapping of the coordinates that you draw to and the coordinates that are displayed on the user's device.
Quartz does make this all fairly straightforward to do in code; the link above has example code.
For example, you could do something like :
CGAffineTransform transform = CGAffineTransformMakeRotation(ANGLE_IN_RADIANS);
view.transform = transform;
And if you want a smoother animation, try using an UIView Animation.
But if you need an other rotation, you should check the documentation :-)
Good Luck !