regarding UIImageView Constant Animation - iphone

I have an image I want to animate constantly in UIImageView. Like some image object to move left and right?
Thanks in advance.

Create the animation in a image editor (or something else if you already have the animation)
Then save each frame as an image. Create a folder in your Xcode project and import the files into it. (in the example below each frame is named "frame-x.png" where x is the frame number)
Then create an array of UIImages and set the UIImageView's animationImages property to it:
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"frame-1"],
[UIImage imageNamed:#"frame-2"],
[UIImage imageNamed:#"frame-3"],
[UIImage imageNamed:#"frame-4"],
nil];
Where you have a UIImage for each frame. You then start the animation:
[imageView startAnimation];
You can also set the animationRepeatCount property of the image view.
See the UIImageView Class Reference for more details.

Related

How to set up a default image

Before i click the image to pull the camera, i want to set up a default image on the imageview that shows a http://www.inc.com/images/avatar/default1.gif to inform that in that imageview goes a person image.
What is the best way to do this procedure?
Best regards.
If you are using a UIImageView the first thing is to create a UIImage object using this code
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
And then pass it to the UIImageView object if it is a property of your view controller you can use this(imageView is the variable name of UIImageView)
self.imageView.image = defaultImage;
If you are instantiating a new UIImageView in your view controller you can use this
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIImageView *theImageView;
//assuming that's the declaration in your header
//make sure default1.gif is inside your application bundle
//copied into the resources folder of your xcode project
//this code goes into your -viewDidLoad method
theImageView.image = [UIImage imageNamed:#"default1.gif"];

Programmatically prevent UIImageView from autoresizing

I would like to truncate one end of a UIImageView by changing the size of the frame dynamically. Of course, if it autoresizes it won't look 'truncated', but will look shrunk. The former is the effect I would like, the latter not.
So far I've set up the UIImageView in IB and then hooked it up programmatically using IBOutlet. I then placed this in ViewDidLoad, but still get the autoresizing:
[self.stringImageView setAutoresizingMask:UIViewAutoresizingNone];
self.stringImageView.frame = CGRectMake(9.0f, 508.0f, 500.0f, 26.0f);
Any ideas how I can clip my UIImageView instead of having it resize?
EDIT 1
It should also be noted that in IB I have deselected Clip Subviews and Autoresize Subviews.
Use : stringImageView.contentMode = UIViewContentModeCenter; so the image you add to the UIImageView always keeps the same size and the same position in your view.
You need to clip the UIImage object first according to imageview's frame and then set it as image for image view. See also Cropping an UIImage
#implementation UIImage (Resize)
// Returns a copy of this image that is cropped to the given bounds.
// The bounds will be adjusted using CGRectIntegral.
// This method ignores the image's imageOrientation setting.
- (UIImage *)croppedImage:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
In Interface Builder set Mode in the View Tab to "Top" or whatever it should be.
Another more efficient way to go if you only wish to make part of the image visible,
or for example animate its appearance,
is to set the content mode of the UIImageView to UIViewContentModeCenter (or any other alignment) and include it in a UIScrollView.
You will only need to change the frame of the scroll view and your image will be visually cropped without you having to create the cropped image (which would be a very high price if it is for animation purpose).

overLap Image by another image

I am new to iphone development and making an application which chooses an image from existing album. After choosing the image I want to put another View on it, or another icon(acne).
Can anyone tell me how to put another image on existing image by code????
Basically if you want to overlay two views/images you can just do it like this:
[self.view addSubview:imageview1];
[self.view addSubview:imageview2];
imageview2 lays on imageview1, because you added it later.
You want to place UIImageView or UIView on the image ?
First you need to create the ImageView with the frame of your choosen image and create another ImageView in the same way as you created first ImageView and add second ImageView to your previous ImageView. if your second ImageView size is equal to the size of first ImageView, you would not be able to see your first ImageView, because it will overlap..
If you want to add a watermark on your image try this code:
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
// This is where we resize captured image
[(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage] drawInRect:CGRectMake(0, 0, 320, 480)];
// And add the watermark on top of it
[[UIImage imageNamed:#"Watermark.png"] drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:WATERMARK_ALPHA];
// Save the results directly to the image view property
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

3D Rotation of Object

I am trying to rotate my object like shakes dice.
please suggest simplest way to implement it in my iphone application.
Any kind of sample code or documentation.
http://www.amazon.com/OpenGL-SuperBible-Comprehensive-Tutorial-Reference/dp/0321498828
If you want to do this without using OpenGL you can use a UIImageView and the set a series of animation images. By creating a series of images you can create a "rolling dice" effect. Here is an example of how to do this:
// create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rollDiceAnimationImage1.png"]];
// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);
// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rollDiceAnimationImage1.png"],
[UIImage imageNamed:#"rollDiceAnimationImage2.png"],
nil];
// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;
// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];
// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];
// start the animation
[self.viewController.rollDiceImageMain startAnimating];
// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];
You can modify the creation and setup including the size, position, number of images, the duration, repeat count, etc as needed. I've used this feature of UIImageView to create simple animation effects and it works great!
Please let me know if you try this and if it works for your needs. Also, post any follow up questions.
Bart
nice your example, but do you think it's possible to rotate / control the rotation with touch gesture ?

UIImageview animation problem?

i have one imageview in IB.i linked it perfectly in my view controller's IBoutlet
which is as IB_img .i have done in my viewdidload as
self.IB_img.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img_1.jpg"],
[UIImage imageNamed:#"img_2.jpg"],nil];
self.IB_img.animationDuration = 1;
self.IB_img.animationRepeatCount = 0;
[self.IB_img startAnimating];
it is not working perfectly....image is not in screen..but if i do it in without IB,in other words dynamically...it works perfectly...any help please to do animation with IB ?
You would have not set the Mose of the imageView as in required format.
You can do it as:
Try to set the one of the animation images as the image of your image view in xib(temporarily).
then set the image view's mode as strecthtofit or whatever suits you. now remove the image. and build you will see the image in proper position.