I need to display array of images in a single view which have various effect in icarousel(time machne,coverflow,etc...)i'm going to use single style where all images have to be displayed..
HERE IS MY CODE IN .M FILE..
UIImage *img=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 290, 400)];
NSLog(#"hi");
img.image=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
But its not working...Can anyone help it out...
Update: As per your latest edit, your question entirely changed, hence my answer does not apply.
Use animationImages instead:
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 290, 400)];
img.animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
From the UIImageView documentation;
animationImages
An array of UIImage objects to use for an animation.
#property(nonatomic, copy) NSArray *animationImages
Discussion The array must contain UIImage objects. You may use the
same image object more than once in the array. Setting this property
to a value other than nil hides the image represented by the image
property. The value of this property is nil by default.
Availability
Available in iOS 2.0 and later.
See Also
#property image
#property contentMode (UIView)
Declared In
UIImageView.h
Have you tried
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"a.png"],
[UIImage imageWithName:#"b.png"],
[UIImage imageWithName:#"c.png"],
[UIImage imageWithName:#"d.png"],
[UIImage imageWithName:#"e.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];
Check out this simple example: http://appsamuck.com/day2.html
EDIT
It appears that the homepage got broken, so check this out instead:
http://web.archive.org/web/20090207210729/http://appsamuck.com/day2.html
Related
I'm wondering if there's anyway to animate an UIImage.
I know that UIImageViews are possible to animate but is there any way to do it directly in a UIImage.
Maybe with Open GL ES or something?
Or are there any other ways you can animate an MPMediaItemArtwork?
Thanks in advance!
Create a UIImageView and set the property of animationImages to an array of UIImages
Here is an example:
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"image1.png"],
[UIImage imageWithName:#"image2.png"],
nil];
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];
If you're targeting iOS 5 you can do this directly in UIImage without the UIImageView using
+(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
for example,
[UIImage animatedImagesWithImages:animationFrames duration:10];
If you mean animation with a few images, use this code:
// create the view that will execute our animation
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
YourImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"01.gif"],
[UIImage imageNamed:#"02.gif"],
[UIImage imageNamed:#"03.gif"],
[UIImage imageNamed:#"04.gif"],
[UIImage imageNamed:#"05.gif"],
[UIImage imageNamed:#"06.gif"],
[UIImage imageNamed:#"07.gif"],
[UIImage imageNamed:#"08.gif"],
[UIImage imageNamed:#"09.gif"],
[UIImage imageNamed:#"10.gif"],
[UIImage imageNamed:#"11.gif"],
[UIImage imageNamed:#"12.gif"],
[UIImage imageNamed:#"13.gif"],
[UIImage imageNamed:#"14.gif"],
[UIImage imageNamed:#"15.gif"],
[UIImage imageNamed:#"16.gif"],
[UIImage imageNamed:#"17.gif"], nil];
// all frames will execute in 1.75 seconds
YourImageView.animationDuration = 1.75;
// repeat the animation forever
YourImageView.animationRepeatCount = 0;
// start animating
[YourImageView startAnimating];
// add the animation view to the main window
[self.view addSubview:YourImageView];
Source
Check UIImage's +animatedImageWithImages:duration: and +animatedImageNamed:duration:.
From UIImage Class Reference:
Creates and returns an animated image from an existing set of images.
This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.
You can use this.
arrayOfImages=[[NSMutableArray alloc]init];
for(int i=1;i<=54;i++)
{
NSString *nameResources=#"haKsequence.png";
NSString *changedString= [NSString stringWithFormat:#"%d", i];
NSString *stringWithoutSpaces = [nameResources stringByReplacingOccurrencesOfString:#"K" withString:changedString];
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
}
self.view.userInteractionEnabled=NO;
i1.hidden=YES;
image1=[[UIImageView alloc]init];
image1.backgroundColor=[UIColor clearColor];
image1.frame = button.frame;//i1 is obshaped buttion
[self.view addSubview:image1];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values = arrayOfMonkeyImages;
[animation setDelegate:self];
animation.repeatCount=3;
[animation setRemovedOnCompletion:YES];
[image1.layer addAnimation:animation forKey:#"animation"];
Swift 4 version:
let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()
In .h file I declare this;
IBOutlet UIImageView *image;
In .m file;
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
image = [[UIImageView alloc] initWithImage:image1];
image.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
and I connected the UIImageView from the Interface builder. But I need to do this Only by code (without using the Interface Builder). Can someone help me modify the code so that I could do this only by code?
I THINK you have some problem in displaying a remote image in uiimageview so i thing u should do that fashion.
NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://image.com/image.jpg"]] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
[image release];
[imageView release];
and i connected the UIImageView from the Interface builder
That was a mistake. If you do that, the image view pointed to by your image instance variable will be the wrong one - the one in the nib. You want it to be the one that you created in code.
So, make no connection from Interface Builder; in fact, delete the image view from Interface Builder so you don't confuse yourself. Make sure your instance variable is also a property:
#property (nonatomic, retain) UIImageView* image;
Synthesize the property:
#synthesize image;
Now your code will work:
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
self.image = [[UIImageView alloc] initWithImage:image1];
// no memory management needed if you're using ARC
[self.view addSubview:self.image];
You will need to play with the frame until the location is correct. Note that the frame will automatically be the same size as the image, by default.
you dont need to connect. This code will work without connecting. Leave IBOutlet out.
UIImage *someImage = [UIImage imageName:#"http://image.com/image.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:someImage];
[self.view addSubView:imageView];
i try to create multiple image array and now i want to add proper tag with specific deck like(Hearts,Diamonds,Spades,Clubs). so in image name as C=dimond, R=Heart,K=spades,F=Clubs and image no as 11=Jack,12=Queen,13=King...so how to distribute it with proper tag so i can indentify specific image.
->>>>>>>>
imagearr =
[[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"C1.png"],
[UIImage imageNamed:#"C2.png"],
[UIImage imageNamed:#"C3.png"],
[UIImage imageNamed:#"C4.png"],
[UIImage imageNamed:#"C5.png"],
[UIImage imageNamed:#"C6.png"],
[UIImage imageNamed:#"C7.png"],
[UIImage imageNamed:#"C8.png"],
[UIImage imageNamed:#"C9.png"],
[UIImage imageNamed:#"C10.png"],
[UIImage imageNamed:#"C11.png"],
[UIImage imageNamed:#"C12.png"],
[UIImage imageNamed:#"C13.png"],
[UIImage imageNamed:#"F1.png"],
[UIImage imageNamed:#"F2.png"],
[UIImage imageNamed:#"F3.png"],
[UIImage imageNamed:#"F4.png"],
[UIImage imageNamed:#"F5.png"],
[UIImage imageNamed:#"F6.png"],
[UIImage imageNamed:#"F7.png"],
[UIImage imageNamed:#"F8.png"],
[UIImage imageNamed:#"F9.png"],
[UIImage imageNamed:#"F10.png"],
[UIImage imageNamed:#"F11.png"],
[UIImage imageNamed:#"F12.png"],
[UIImage imageNamed:#"F13.png"],
[UIImage imageNamed:#"K1.png"],
[UIImage imageNamed:#"K2.png"],
[UIImage imageNamed:#"K3.png"],
[UIImage imageNamed:#"K4.png"],
[UIImage imageNamed:#"K5.png"],
[UIImage imageNamed:#"K6.png"],
[UIImage imageNamed:#"K7.png"],
[UIImage imageNamed:#"K8.png"],
[UIImage imageNamed:#"K9.png"],
[UIImage imageNamed:#"K10.png"],
[UIImage imageNamed:#"K11.png"],
[UIImage imageNamed:#"k12.png"],
[UIImage imageNamed:#"K13.png"],
[UIImage imageNamed:#"R1.png"],
[UIImage imageNamed:#"R2.png"],
[UIImage imageNamed:#"R3.png"],
[UIImage imageNamed:#"R4.png"],
[UIImage imageNamed:#"R5.png"],
[UIImage imageNamed:#"R6.png"],
[UIImage imageNamed:#"R7.png"],
[UIImage imageNamed:#"R8.png"],
[UIImage imageNamed:#"R9.png"],
[UIImage imageNamed:#"R10.png"],
[UIImage imageNamed:#"R11.png"],
[UIImage imageNamed:#"R12.png"],
[UIImage imageNamed:#"R13.png"],nil];
..............
There's a better work around this issue rather than taking a bunch of images in the array. Initialize an Enum with all different deck categories and loading the image through dynamically generated name for e.g:
// In a header file
typedef enum {
HEARTS, // Hearts
DIAMONDS, // Diamonds
SPADES, // Spades
CLUBS // Club
} DeckType;
// In a source file
NSString * const DeckType_toString[] = {
#"H",
#"D",
#"S",
#"C"
};
- (UIImage *)getDeckImage:(DeckType)deckType withCardNo:(NSInteger)cardNo {
return [UIImage imageNamed:[NSString stringWithFormat:#"%#%d.png", DeckType_toString[deckType], cardNo]];
}
You can also take another enum with group of 1...13 cards and used them instead of cardNo
I am not sure,you could create a UIImage with tag .because it's inherited from NSObject NOT UIView which provide you an integer tag variable and used by the UIView class.
But still you could do it by writing a wrapper for you UIView class, and should be like below
use below as reference.
#interface MyUIImageWrapper: NSObject
{
UIImage* iMyImage;
int tag;
}
you could have some init function which take two argument one for image name and others for tag value
Like below
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag ;
And the implementation for the method.
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag
{
if(self = [super init])
{
iMyImage = [UIImage imageNamed:fileName];
tag = myTag;
}
return self;
}
To identify images in a collection I'm suggesting an NSDictionary instead of an NSArray so every image is associated with an NSString, e.g. [[NSDictionary alloc] initWithObjectsAndKeys:..., [UIImage imageNamed:#"R7.png"], #"R7", ...]; tagging works for UIImageViews (all UIView descendants) and is more proper for retrieving already added subviews...
UIImage *image = [[UIImage alloc] initWithContentsOfFile:#"popup.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[image release];
[imageView release];
The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.
I'm tired.
initWithContentsOfFile requires the complete file path, not just the file name.
Replace the line where you initialize UIImage with the following:
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:#"popup" ofType:#"png"]];
A simpler way will be to use the imageNamed: method of the UIImage class
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popup.png"]];
And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.
Hope this helps!
Try to use:
UIImage *image = [UIImage imageNamed:#"popup.png"];
and, off course, get rid of the [image release]; because in this solution the image is autoreleased.
Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...
your imageView needs a frame.
imageView.frame = CGRectMake(0,0,320,480);
I know this question is old now, but some users can still benefit from an answer.
Like others have said, try using [UIImage imagedNamed:] instead of initWithContentsOfFile.
And the reason it is not showing up on screen is you did not set a location for the image. You can fix this by either setting a frame for the image view or setting its center at a single point. To place the image view in the center of the screen:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = [self.view center];
// or if you want to set a frame instead
// CGRect imageFrame = CGRectMake(x,y,width,height);
// [imageView setFrame:imageFrame];
[self.view addSubView:imageView];
Enjoy :)
I have a few UIButtons that I'd like to trigger short 5-10 frame animations when pressed in the middle of the screen along with other nonrelated stuffs. Can I just drop one UIImageView in IB and update it, somehow, programmatically? It doesn't need to be overly complex, I don't even mind if it's hard coded, for now since it's such a small project.
Thanks.
Here's an example of the easiest way you do an animation with just a few frames is:
Declare an IBOutlet UIImageView *timerAnimation and link it to your UIIMageView in IB. Add some number of images to your resources, such as you see needed below. Then load the images into an array - see the code:
timerAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"timer00.png"],
[UIImage imageNamed:#"timer01.png"],
[UIImage imageNamed:#"timer02.png"],
[UIImage imageNamed:#"timer03.png"],
[UIImage imageNamed:#"timer04.png"],
[UIImage imageNamed:#"timer05.png"],
[UIImage imageNamed:#"timer06.png"],
[UIImage imageNamed:#"timer07.png"],
[UIImage imageNamed:#"timer08.png"],
[UIImage imageNamed:#"timer09.png"],
[UIImage imageNamed:#"timer10.png"],
[UIImage imageNamed:#"timer11.png"],
[UIImage imageNamed:#"timer12.png"],
[UIImage imageNamed:#"timer13.png"],
[UIImage imageNamed:#"timer14.png"],
[UIImage imageNamed:#"timer15.png"],
[UIImage imageNamed:#"timer16.png"],
[UIImage imageNamed:#"timer17.png"],
[UIImage imageNamed:#"timer18.png"],
[UIImage imageNamed:#"timer19.png"],
[UIImage imageNamed:#"timer20.png"],
[UIImage imageNamed:#"timer21.png"],
[UIImage imageNamed:#"timer22.png"],
[UIImage imageNamed:#"timer23.png"],
[UIImage imageNamed:#"timer24.png"],
[UIImage imageNamed:#"timer25.png"],
[UIImage imageNamed:#"timer26.png"],
[UIImage imageNamed:#"timer27.png"],
[UIImage imageNamed:#"timer28.png"],
[UIImage imageNamed:#"timer29.png"],
[UIImage imageNamed:#"timer30.png"],
[UIImage imageNamed:#"timer31.png"],
[UIImage imageNamed:#"timer32.png"],
[UIImage imageNamed:#"timer33.png"],
[UIImage imageNamed:#"timer34.png"],
[UIImage imageNamed:#"timer35.png"],
// [UIImage imageNamed:#"timer36.png"], save last picture for "times up"
nil];
timerAnimation.animationDuration = 5.0;
timerAnimation.animationRepeatCount = 1;
[timerAnimation startAnimating];
This will run your animation until you call [timerAnimation stopAnimating];
Yes. Create an IBOutlet to your UIImageView and you can update it from code. For example, use its image property to set the UIImage that should be displayed. Or you can add the image to your app's resources and associate it with the UIImageView in Interface Builder. Use .png files since the iPhone has been optimized for them.
Since a UIImageView is a UIView, all the usual Core Animation methods can be used.