I have an amination that works on an iPhone but when I converted the project over to an iPad it doesn't show up or play.
containerArray14 = [NSArray array];
containerArray14 =[[NSArray alloc ]initWithObjects:[UIImage imageNamed:#"moon1.png"],
[UIImage imageNamed:#"moon2.png"],[UIImage imageNamed:#"moon3.png"],
[UIImage imageNamed:#"moon4.png"],[UIImage imageNamed:#"moon5.png"],
[UIImage imageNamed:#"moon6.png"],[UIImage imageNamed:#"moon7.png"],
[UIImage imageNamed:#"moon8.png"],[UIImage imageNamed:#"moon9.png"],
[UIImage imageNamed:#"moon10.png"],[UIImage imageNamed:#"moon11.png"],nil];
stop2.animationImages = containerArray14;
stop2.animationDuration = 2.0;
stop2.animationRepeatCount = FLT_MAX;
[stop2 startAnimating];
this is the code I use in viewDidLoad and it works great on the iPhone but nothing on the iPad.
if I make the uiimageview (stop2) just a still image it loads fine. On the phone I have 10 of these going at the same time. On the iPad I can't get even one to work.
Any ideas
Related
I am currently using a UIImageView to animate a series of images depending on a button pressed by the user. I have about 5 different sets of images, that i have created in NSArrays, such as this:
-(void)initiateAnimations {
punchani = [NSArray arrayWithObjects:
[UIImage imageNamed:#"punch0001.png"],
[UIImage imageNamed:#"punch0002.png"],
[UIImage imageNamed:#"punch0003.png"],
[UIImage imageNamed:#"punch0004.png"],
[UIImage imageNamed:#"punch0005.png"],
[UIImage imageNamed:#"punch0006.png"],
[UIImage imageNamed:#"punch0007.png"],
[UIImage imageNamed:#"punch0008.png"],
[UIImage imageNamed:#"punch0009.png"],
[UIImage imageNamed:#"punch0010.png"],
[UIImage imageNamed:#"punch0011.png"],
[UIImage imageNamed:#"punch0012.png"],nil];
}
And then i am using the standard animation code to loop them once:
player.animationImages = punchani;
player.animationDuration = 0.50;
player.animationRepeatCount = 1;
[player startAnimating];
However, as i load more and more/different images into the UIImageView, i eventually get an output that a Memory warning as follows: (subsituted my actual executable with 'appname')
2013-03-15 20:52:23.065 AppName[2080:907] Received memory warning.
I knew this would probably happen, but ARC forbids me from releasing the arrays. How can i fix this leak? thanks
UIImage imageNamed: caches the images and will release the memory on it's own schedule. Use
+ (UIImage *)imageWithContentsOfFile:(NSString *)path to load memory directly
Try this answer as well. Remove array of images when animation is over
Have a little design issue after having upgraded to iOS 5 and Xcode 4.2
This is how my view looked in iOS 4:
1 http://casperslynge.dk/1
And this is how it looks like in iOS 5:
2 http://casperslynge.dk/2
In my navigation delegate I have the following method to draw the "image" at the top:
- (void)drawRect:(CGRect)rect {
UIImage *image;
if(self.barStyle == UIBarStyleDefault){
image = [UIImage imageNamed: #"topbar_base.png"];
}
else{
image = [UIImage imageNamed: #"nyhedsbar_base.png"];
}
[image drawInRect:CGRectMake(-1, -1, self.frame.size.width+3, self.frame.size.height+3)];
}
And inside my controller I set the following:
self.navigationBarStyle = UIBarStyleBlack;
How come it is not working in iOS 5?
Thanks
Under iOS5, you need to use UIAppearance. Have a look at that. Here's an example for using it conditionally so that you can continue to support iOS4:
// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)]) {
UIImage *img = [UIImage imageNamed: #"NavBarBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
As you can see, this sets a custom background image for all UINavigationBars. There are lots of things you can do with UIAppearance. You'll want to keep any custom stuff you're currently doing in drawRect: since pre-iOS4 devices will still use that and not the new UIAppearance code.
I have a UITable View that displays an image in the left hand side of the table cell, and it works fine in the simulator.
Only problem is, once I ran it on my device no images appear. It's just a blank white space.
Have checked that images are added to resource folder for build (which they are) and that capitals etc. match (which they do).
Any ideas?
Thanks.
Code to display images:
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
UIImage *image = [UIImage imageNamed:[[dog types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];;
if ( image ) {
cell.imageView.image = [image imageScaledToSize:CGSizeMake(50, 50)];
}
This is the problem of images, not your code (if they are showing in the simulator). Many times when the images are not in proper format (so that iPhone can understand) and many times the renaming does the bad job (suppose you have changed the extension without changing the format).
You can not compare a simulator with iPhone, because simulator takes advantages of mac-os, so it can do some things that iPhone can't.
So the moral of the story is re-create your images, and check your code with some other images(images that had been showing on device in other projects).
Thanks,
Madhup
Check the iPhone SDK version on which you are running in XCode.
it should be same as the iPhone OS of your iPhone contains.
May be this was the issue...
you can have another sollution for getting rounded images if you want to try :
UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
replace you code with this and see the log in console :
NSString *strImgName = [[dog types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(#"%#",strImgName);
UIImage *image = [UIImage imageNamed:strImgName];
and see what you are getting as an image name and mach it with your actual image in resource folder...it should be same (i.e. apple.png = apple.png) including *.extensions
Try this, if image is in the resource group.
UIImage *cellImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[dog types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ofType:#"png"]];
I know, that's quite an old post now, but I think I have to share what my problem was, somewhere
In my case I had the following code
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"some-Background.png"]];
[tempImageView setFrame:self.tableView.frame];
[self.tableView setBackgroundView:tempImageView];
but my Image was named "some-background.png".
In the simulator everything looked fine, but on the phone I got no background. It seems that the simulator is case insensitive, whereas the iPhone is not.
I am creating an application for the iPhone which involves having more than one button which all animate the same UIImageView. It works well in the simulator, (like practically all apps) but when it comes to the device it plays the animation well, but with repetitive pressing of the buttons the app quits. So far I have implemented 2 buttons. Here's what happens when they are pressed.
Okay, I have 44 MB of ram available when my app starts, Then when I press the first of 2 buttons that start an animation the available memory goes down to 31 and climbs up to 32, then when I press the second button the available memory goes down to 9 then climbs to 24 and then strangely declines to 10 slowly. If the first button is then pressed then the available memory climbs to 14MB And under repetitive pressing of these buttons the apps memory goes down to 4 to 3 MB and quits. In instruments there are no leaks. Here is my code incase anyone can spot where my memory issue lurks. (BTW, I'm still developing on 2.2.1 because if I upgrade to 3.0 i won't be able to test my apps on the device as I am not enrolled in the apple developer program yet and I followed an online tutorial to get apps onto the device that only works with 2.2.1)
#synthesize 123pig;
- (IBAction)startClick:(id)sender{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:#selector(tick) userInfo:nil repeats:NO];
123pig.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"123pigapple0001.png"],
[UIImage imageNamed: #"123pigapple0002.png"],
[UIImage imageNamed: #"123pigapple0003.png"],
[UIImage imageNamed: #"123pigapple0004.png"],
[UIImage imageNamed: #"123pigapple0005.png"],
[UIImage imageNamed: #"123pigapple0006.png"],
[UIImage imageNamed: #"123pigapple0007.png"],
[UIImage imageNamed: #"123pigapple0008.png"],
[UIImage imageNamed: #"123pigapple0009.png"],
[UIImage imageNamed: #"123pigapple0010.png"],
[UIImage imageNamed: #"123pigapple0011.png"],
[UIImage imageNamed: #"123pigapple0013.png"],
[UIImage imageNamed: #"123pigapple0014.png"],
[UIImage imageNamed: #"123pigapple0015.png"],
[UIImage imageNamed: #"123pigapple0016.png"],
[UIImage imageNamed: #"123pigapple0017.png"],
[UIImage imageNamed: #"123pigapple0018.png"],
[UIImage imageNamed: #"123pigapple0019.png"],
[UIImage imageNamed: #"123pigapple0020.png"],nil];
[123pig setAnimationRepeatCount:1];
123pig.animationDuration =.7;
[123pig startAnimating];
}
- (void)tick{
[self animatePig];
}
- (void)animatePig{
UIImage *pigImage13=[UIImage imageNamed:#"123pigapple0020.png"];
if(123pig.image == pigImage13)
123pig.image = pigImage13;
else
123pig.image = pigImage13;
}
- (IBAction)startClick1:(id)sender{
animationTimer1 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:#selector(tick1) userInfo:nil repeats:NO];
123pig.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"123pig0015.png"],
[UIImage imageNamed: #"123pig0016.png"],
[UIImage imageNamed: #"123pig0017.png"],
[UIImage imageNamed: #"123pig0018.png"],
[UIImage imageNamed: #"123pig0019.png"],
[UIImage imageNamed: #"123pig0020.png"],
[UIImage imageNamed: #"123pig0021.png"],
[UIImage imageNamed: #"123pig0022.png"],
[UIImage imageNamed: #"123pig0023.png"],
[UIImage imageNamed: #"123pig0024.png"],
[UIImage imageNamed: #"123pig0025.png"],
[UIImage imageNamed: #"123pig0026.png"],
[UIImage imageNamed: #"123pig0027.png"],
[UIImage imageNamed: #"123pig0028.png"],
[UIImage imageNamed: #"123pig0029.png"],
[UIImage imageNamed: #"123pig0030.png"],
[UIImage imageNamed: #"123pig0031.png"],
[UIImage imageNamed: #"123pig0032.png"],
[UIImage imageNamed: #"123pig0033.png"],
[UIImage imageNamed: #"123pig0034.png"],
[UIImage imageNamed: #"123pig0035.png"],
[UIImage imageNamed: #"123pig0036.png"],
[UIImage imageNamed: #"123pig0037.png"],
[UIImage imageNamed: #"123pig0038.png"],
[UIImage imageNamed: #"123pig0039.png"],
[UIImage imageNamed: #"123pig0040.png"],
[UIImage imageNamed: #"123pig0041.png"],
[UIImage imageNamed: #"123pig0042.png"],
[UIImage imageNamed: #"123pig0043.png"],
[UIImage imageNamed: #"123pig0044.png"],
[UIImage imageNamed: #"123pig0045.png"],
[UIImage imageNamed: #"123pig0046.png"],
[UIImage imageNamed: #"123pig0047.png"],
[UIImage imageNamed: #"123pig0048.png"],
nil];
[123pig setAnimationRepeatCount:1];
123pig.animationDuration =2.7;
[123pig startAnimating];
}
- (void)tick1{
[self animatePig1];
}
- (void) animatePig1{
UIImage *pigImage11=[UIImage imageNamed:#"123pig0048.png"];
if(123pig.image == pigImage11)
123pig.image = pigImage11;
else
123pig.image = pigImage11;
}
- (void)stopTimer
{
[animationTimer invalidate];
[animationTimer release];
[animationTimer1 invalidate];
[animationTimer1 release];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
123pig.center = location;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
[123pig release];
}
#end
I have an idea that it's imageNamed thats causing it and that 2.2.1 doesn't clear it's cache even after a memory warning, and on another SO question someone said "My understanding is that the +imageNamed: cache should respect memory warnings on iPhone OS 3.0. Test it when you get a chance and report bugs if you find that this is not the case." Now even if i did download the firmware and SDK of 3.0 I wouldn't know if it was clearing the cache of imageNamed because I wouldn't be able to test it on a device! BTW when answering keep in mind that I am new to The iPhone sdk. Thanks in advance.
----------------Edit----------------
I've just been accepted into the apple iphone developer program. WOOOHOOO, I'll check if 3.0 releases cached images properly.
-------------Edit------------------
Yes it does release cached images. Check my answer below for further clarification.
Okay people, It would seem that iPhone OS 3.0 does clear cached images on a did receive memory warning command, wooohooo! Me 1, Xcode 0. A bit more detail: In the iPhone os 2.2.1 when you release an animated Image view, the cached images aren't released, so if your app had loads of animations then eventually the iPhone would run out of memory and quit. So you would just have to go into other more complex methods of animation, and no one wants to do that if they can avoid it. But now releasing does clear the cache! I can tell because when I press a button in my app to animate a view there's a bit of a wait until the animation plays, then if that is pressed again there is no wait (because it cached it) But then if you press another button that animates that view, the previous cached animation is released! So pressing the first button gives you that original wait, so I can relax now and get on with developing rather then trying to fix a memory management issue that boils down to be apples fault.
I'm really confused about the behavior of your timers. You start the pig UIImageView animating, and then 1/30th of a second later, your timer fires and it sets the pig image to something else?
You may want to consolidate these two methods of animation (UIImageView animationImages and manual animation using timer). UIImageView really isn't meant to play long animations (see this question). I think it preloads all the images at once when you start the animation - thus the huge memory hit.
A better bet might be to use a timer for everything and keep a local variable corresponding to the current frame. When your timer fires, you could load just one image using imageNamed: and apply it to the UIImageView. I'm not sure what the caching behavior of UIImage is, but I'm pretty sure piling 100 of them into a UIImageView is a bad idea.
Is there any chance you could reduce the frame rate of your animation as well? It seems you're trying to flip through all 100 images in the pig's animation in 2.7 seconds - that's 37 fps! 24 fps would probably be fine and help lower your memory footprint.
Also - this is off topic, but I noticed you're calling [123pig dealloc] in your dealloc function. In general, you should just call release, and dealloc will be called automatically if the object's retain count is zero (meaning the object is no longer used). Manually calling it could cause you to destroy things that another object in the app is still using. Probably doesn't matter in this case, but it could cause some nasty bugs in other scenarios.
Sorry I don't have a definite answer on the cache issue. Hope that helps a bit!
Your dealloc code is incorrect. You should only be releasing 123pig, not deallocating it. You should do so before calling [super dealloc] since after that call, all your instance variables (incling 123pig) may be invalidated. This may have manifested itself as not releasing the image memory since there is a dangling reference to 123pig.
I have an animated gif file that I want to use in my iPhone application, but the animation doesn't work. Does anybody know how to fix it?
If you have a serie of images you want to animate you can easily do it with UIImageView:
UIImage *blur5 = [UIImage imageNamed:#"blur5.png"];
UIImage *blur6 = [UIImage imageNamed:#"blur6.png"];
self.imageView.animationImages = [[NSArray alloc] initWithObjects:blur5, blur6, nil];
self.imageView.animationRepeatCount = 5;
[self.imageView startAnimating];
I found this easier than trying to use UIWebView.
UIWebView does not properly display all GIF content. You need to use a UIImageView, but the iPhone OS does not support animated GIFS and only displays the first frame.
So you need to extract all of the other frames first.
Crude example code here:
http://pliep.nl/blog/2009/04/iphone_developer_decoding_an_animated_gif_image_in_objc
You can use source at http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easy/
It has a GIF decoder that you can use directly to get solution.
I successfully used it. But it have some problems with transparency.
This can be achieved by this piece of code:
NSArray * imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], nil]; //this will be the frames of animation images in sequence.
ringImage = [[UIImageView alloc]initWithFrame:CGRectMake(100,200,600,600)];
ringImage.animationImages = imageArray;
ringImage.animationDuration = 1.5;//this the animating speed which you can modify
ringImage.contentMode = UIViewContentModeScaleAspectFill;
[ringImage startAnimating];//this method actually does the work of animating your frames.
I know its an old thread..but may be helpful for someone..:)
One other option is to decode the gif in your application, and then "frame serve" it to a OpenGL object. This way is less likely to run out of memory for large gifs.