In my app, i have a little problem in memory issue.
My coding skill is not enough to perfect.
My code use memory four time that i really need.
How can i change my coding?
My Code is
-(void)viewDidAppear:(BOOL)animated
{
UIScrollView * ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];// 44(UpperNavigtionBar),-88(Upper+Lower)
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
UIImage *image5 = [UIImage imageNamed:#"Image-005.jpg"];
UIImage *image6 = [UIImage imageNamed:#"Image-006.jpg"];
UIImage *image7 = [UIImage imageNamed:#"Image-007.jpg"];
UIImage *image8 = [UIImage imageNamed:#"Image-008.jpg"];
UIImage *image9 = [UIImage imageNamed:#"Image-009.jpg"];
UIImage *image10 = [UIImage imageNamed:#"Image-010.jpg"];
UIImage *image11 = [UIImage imageNamed:#"Image-011.jpg"];
UIImage *image12 = [UIImage imageNamed:#"Image-012.jpg"];
UIImage *image13 = [UIImage imageNamed:#"Image-013.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,nil];
NSInteger numberOfViews = 13;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView * ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height-88); // -88(for adding Image View as subview)
[self.view addSubview:ScrollView];
}
There are several things you can do to limit your memory usage. First, you should fill your array with the names of the images, not the images themselves. Given the way your images are named, you should be able to do this in a loop. Second, when you load the image, use imageWithContentsOfFile: instead of imageNamed:. The latter method caches images, while the former doesn't. If you use imageNamed: the memory usage will continue to grow as you scroll through images (even with lazy loading) because of the caching -- at some point the system should purge the cache, which should keep your app from crashing, but I don't think it will keep the system for shutting down other programs that are in the background on a device.
Related
I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];
For starters I am trying to animate my main character of the game so I can later replace the images in the array with sprites -
Ive done this in my header file for my main character :
- (void)Animate;
And in my implementation file ive written :
-(void) Animate{
UIImage* img1 = [UIImage imageNamed:#"Obstacle.png"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02.png"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03.png"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)];
//images.name = #"animation";
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:0];
[imageView setAnimationDuration:0.5];
//imageView.center = character.center ;
[imageView startAnimating];
// character used to be myView
// the images inside the array !
}
Take note I am using the game feature of Xcode .
So what am I not doing and how can I fix it ?
UIImage* img1 = [UIImage imageNamed:#"Obstacle"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04"];
NSArray *animateArray = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 160)];
self.imageView.animationImages = animateArray;
self.imageView.animationDuration = 3.0;
[self.imageView startAnimating];
self.imageView.animationRepeatCount = 0;
[self.view addSubview:self.imageView];
In our iOS app, we want a loading image which is just our logo, animated. I'm wondering what the best practice is to do this? Would it be to use a series of .png images, or one long .png image. I can also thing of using a .gif but the colour quality is not the greatest.
It may also be possible, since the animation is just resizing of certain elements, to do the animation programatically using several UIimages and resizing them, though it may not be as smooth.
UIImage* img1 = [UIImage imageNamed:#"image_1.png"];
UIImage* img2 = [UIImage imageNamed:#"image_2.png"];
UIImage* img3 = [UIImage imageNamed:#"image_3.png"];
UIImage* img4 = [UIImage imageNamed:#"image_4.png"];
UIImage* img5 = [UIImage imageNamed:#"image_5.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4,img5,nil];
UIImageView* imageView = [[UIImageView alloc]
initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 50, 80.0, 80.0)];
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:100];
[imageView setAnimationDuration:3.0];
[imageView startAnimating];
imageView.tag = 100;
[self.view addSubview:imageView];
i have retrived images from document directory and want to display that images in scrollview. the images is coming from the document directory but not displaying in scrollview. i have the below code. I have put scrollview graipically on xib and also creat outlet..
self.scrollView.contentSize = CGSizeMake(320, 136);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
//imge.image=img;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 100, 100)] ;
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 140);
}
Seems you are trying to create the UIImageView from images that are located in your resources (but they are not there!).
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
whats the point of placing the images in a array, if you dont use it?
try this instead:
[imageView setImage: [images objectAtIndex:i]];
I have the following in my Navigation Controller Load event...
UIImage *image = [UIImage imageNamed:#"Background.jpg"];
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 500, 620));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* imageView = [[UIImageView alloc] initWithImage:part];
[self.view insertSubview:imageView atIndex:0];
This sets the background image and works fine once I have set the embedded view to have a clear background. My question is how do I alter this image once it is loaded?
The effect I am looking for is that I want to crop a new section of the image when different views are pushed onto the stack.
I know I could load a new image for each table but the desired effect I want is so that when you push to the next view, the table moves from right to left showing the new table, but the background moves right to left only a little bit.
Ended up using NSTimer and this combined
[[self.navigationController.view.subviews objectAtIndex:0] removeFromSuperview];
[self.navigationController.view insertSubview:imageView atIndex:0];
Use tag to different your backgroundView and other views
#deifne BACKGROUNDVIEWTAG 999
if(![self.view subviews] || ((UIView *)[[self.view subviews] objectAtIndex:0]).tag != BACKGROUNDVIEWTAG)
{
UIImage *image = [UIImage imageNamed:#"Background.jpg"];
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 500, 620));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* imageView = [[UIImageView alloc] initWithImage:part];
imageView.tag = BACKGROUNDVIEWTAG;
[self.view insertSubview:imageView atIndex:0];
}