How to make UIImageView animation power efficient? - ios

So I have got this method in my code that shows a really nice loader in my app. The best way I was told to do this, is with the UIImageView.
I set up all the parameters, and I think I have covered everything I needed to. The loader continues to 'spin' in the background of my app, and corresponding UI elements adjust their alpha, based on when I need to show the loader, in the case the UITableView.
However, when testing on iOS 7 and profiling energy diagnostics and time profiling, I found that it takes a whole lot out of the app. Is there any way to make this more efficient?
- (void)displayLoadingCircle:(BOOL)display {
UIView *placeholderView = [[UIView alloc] initWithFrame:self.view.bounds];
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(94, 240, 125, 125)];
[animatedImageView setContentMode:UIViewContentModeScaleToFill];
[animatedImageView setAlpha:0.7f];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"loader-1 (dragged).png"],
[UIImage imageNamed:#"loader-2 (dragged).png"],
[UIImage imageNamed:#"loader-3 (dragged).png"],
[UIImage imageNamed:#"loader-4 (dragged).png"],
[UIImage imageNamed:#"loader-5 (dragged).png"],
[UIImage imageNamed:#"loader-6 (dragged).png"],
[UIImage imageNamed:#"loader-7 (dragged).png"],
[UIImage imageNamed:#"loader-8 (dragged).png"],
[UIImage imageNamed:#"loader-9 (dragged).png"],
[UIImage imageNamed:#"loader-10 (dragged).png"],
[UIImage imageNamed:#"loader-11 (dragged).png"],
[UIImage imageNamed:#"loader-12 (dragged).png"],
[UIImage imageNamed:#"loader-13 (dragged).png"],
[UIImage imageNamed:#"loader-14 (dragged).png"],
[UIImage imageNamed:#"loader-15 (dragged).png"],
[UIImage imageNamed:#"loader-16 (dragged).png"],
[UIImage imageNamed:#"loader-17 (dragged).png"],
[UIImage imageNamed:#"loader-18 (dragged).png"],
[UIImage imageNamed:#"loader-19 (dragged).png"],
[UIImage imageNamed:#"loader-20 (dragged).png"],
[UIImage imageNamed:#"loader-21 (dragged).png"],
[UIImage imageNamed:#"loader-22 (dragged).png"],
[UIImage imageNamed:#"loader-23 (dragged).png"],
[UIImage imageNamed:#"loader-24 (dragged).png"],
[UIImage imageNamed:#"loader-25 (dragged).png"],
[UIImage imageNamed:#"loader-26 (dragged).png"],
[UIImage imageNamed:#"loader-27 (dragged).png"],
[UIImage imageNamed:#"loader-28 (dragged).png"],
[UIImage imageNamed:#"loader-29 (dragged).png"],
[UIImage imageNamed:#"loader-30 (dragged).png"], nil];
animatedImageView.animationDuration = 0.55f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[placeholderView addSubview:animatedImageView];
[self.view insertSubview:placeholderView belowSubview:_tweetTable];
self.placeholderView = placeholderView;
_tweetTable.alpha = 0.0f;
}

Turns out my frame rate was too high (60 fps) which made the iPhone do too much work. Reducing the duration to 1.0f at 30 frames, decreased the frames per second to 30 fps which solved my issue.

Related

Why is my NSArray returning an inaccurate count with arrayWithObjects of UIImages?

I'm completely lost with this. I'm receiving a nil error so I tried to print out the count of my NSArray. I have 20 UIImages initialized in the array, which should return a count of 20, but it returns a count of 17.
images = [NSArray arrayWithObjects:[UIImage imageNamed:#"1"],
[UIImage imageNamed:#"2"],
[UIImage imageNamed:#"3"],
[UIImage imageNamed:#"4"],
[UIImage imageNamed:#"5"],
[UIImage imageNamed:#"6"],
[UIImage imageNamed:#"7"],
[UIImage imageNamed:#"8"],
[UIImage imageNamed:#"9"],
[UIImage imageNamed:#"10"],
[UIImage imageNamed:#"11"],
[UIImage imageNamed:#"12"],
[UIImage imageNamed:#"13"],
[UIImage imageNamed:#"14"],
[UIImage imageNamed:#"15"],
[UIImage imageNamed:#"16"],
[UIImage imageNamed:#"17"],
[UIImage imageNamed:#"18"],
[UIImage imageNamed:#"19"],
[UIImage imageNamed:#"20"], nil ];
When I do a [images count], it returns 17.
Why is this? Why does it stop counting at 17? Thanks
Your app would not be having any image named 18,19 and 20 and therefore when you add object to array as [UIImage imageNamed:#"18"], it takes nil object.

Error when load images like gif in uiimageview

I want to load images like gif. It work perfectly when i go to viewcontroller by push segue but when i go to viewcontroller by modal segue not load images in imageview.
_ivDummy.animationImages = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
nil];
[_ivDummy setBackgroundColor:[UIColor redColor]];
_ivDummy.animationDuration = 1.0f;
_ivDummy.animationRepeatCount = INFINITY;
[_ivDummy startAnimating];
Please, Help me.
In Progress Hud
[indicator removeFromSuperview];
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 45.0f, 45.0f)];
animatedImageView.animationImages = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
nil];
//[animatedImageView setBackgroundColor:[UIColor redColor]];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = INFINITY;
[animatedImageView startAnimating];
indicator=animatedImageView;
[self addSubview:indicator];
Instead of using
- (void)viewDidLoad method try using
- (void)viewWillAppear:(BOOL)animated
-(void)viewWillAppear:(BOOL)animated
{
_ivDummy.animationImages = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
nil];
[_ivDummy setBackgroundColor:[UIColor redColor]];
_ivDummy.animationDuration = 1.0f;
_ivDummy.animationRepeatCount = INFINITY;
[_ivDummy startAnimating];
}
You can override this method to perform custom tasks associated with displaying the view.

How to add more than 6 segments with image on iphone using uisegmentcontrol

The thing is i can add segments as much as i want if i use text but if i use image means i can not able to add more than 6 segments on iphone.
help me out.
UIImage* resetImage = [UIImage imageNamed:#"Reset_V1"];
UIImage* wlwwImage = [UIImage imageNamed:#"Reset_V1"];
UIImage* huImage = [UIImage imageNamed:#"HU_PI_V1"];
UIImage* panImage = [UIImage imageNamed:#"Panning_V1"];
UIImage* disImage = [UIImage imageNamed:#"Distance_V1"];
UIImage* areaImage = [UIImage imageNamed:#"Reset_V1"];
UIImage* clrImage = [UIImage imageNamed:#"Clear_V1"];
UIImage* presetImage = [UIImage imageNamed:#"Presets_V1"];
mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage, nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[segmentControl addTarget:self action:#selector(whichSegment:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
I try with same code and it work
Output:
Code:
UIImage* resetImage = [UIImage imageNamed:#"Image"];
UIImage* wlwwImage = [UIImage imageNamed:#"Image"];
UIImage* huImage = [UIImage imageNamed:#"Image"];
UIImage* panImage = [UIImage imageNamed:#"Image"];
UIImage* disImage = [UIImage imageNamed:#"Image"];
UIImage* areaImage = [UIImage imageNamed:#"Image"];
UIImage* clrImage = [UIImage imageNamed:#"Image"];
UIImage* presetImage = [UIImage imageNamed:#"Image"];
UIImage* presetImage1 = [UIImage imageNamed:#"Image"];
UIImage* presetImage2= [UIImage imageNamed:#"Image"];
UIImage* presetImage3= [UIImage imageNamed:#"Image"];
NSArray *mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage,presetImage1,presetImage2,presetImage3, nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[self.view addSubview:segmentControl];
Download demo project
Link
If your image path is not valid then it will show only segment with correct image
Code(with bug)
// Do any additional setup after loading the view, typically from a nib.
UIImage* resetImage = [UIImage imageNamed:#"Image"];
UIImage* wlwwImage = [UIImage imageNamed:#"Image"];
UIImage* huImage = [UIImage imageNamed:#"Image"];
UIImage* panImage = [UIImage imageNamed:#"Image"];
UIImage* disImage = [UIImage imageNamed:#"Image"];
UIImage* areaImage = [UIImage imageNamed:#"Image1"]; // wrong image name
UIImage* clrImage = [UIImage imageNamed:#"Image1"]; // wrong image name
UIImage* presetImage = [UIImage imageNamed:#"Image1"]; // wrong image name
UIImage* presetImage1 = [UIImage imageNamed:#"Image1"]; // wrong image name
UIImage* presetImage2= [UIImage imageNamed:#"Image1"]; // wrong image name
UIImage* presetImage3= [UIImage imageNamed:#"Image1"]; // wrong image name
NSArray *mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage,presetImage1,presetImage2,presetImage3, nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[self.view addSubview:segmentControl];

SDImageCache placeholder gif

I am trying to create a gif as a place holder for my SDImageCache.
So first off I created the gif:
_imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.thumbnailImageView.frame.size.width,
cell.thumbnailImageView.frame.size.height)];
_imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Polygon 1 copy.bolt_00000"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00000"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00001"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00002"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00003"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00004"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00005"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00006"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00007"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00008"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00009"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00010"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00011"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00012"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00013"], [UIImage imageNamed:#"Polygon 1 copy.bolt_00014.png"], nil];
_imageView.animationRepeatCount = 500;
I then turned it into an UIimage
UIImage *imagety = _imageView.image; // get the UIImage
UIImage *otherImage = [UIImage imageNamed:#"Loadimage.png"]; // load another image
_imageView.image = otherImage; // change in the image in your UIImageView
following that I put it as the placeholder:
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache queryDiskCacheForKey:encodedString done:^(UIImage *image, SDImageCacheType cacheType) {
if (image){
cell.thumbnailImageView.image = image;
}else{
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString] placeholderImage:imagety completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[imageCache storeImage:image forKey:encodedString];
}];
}
}];
But this does not seem to work? Do you have any ideas why?
If you check the value returned by _imageView.image:
UIImage *imagety = _imageView.image;
you should see that it is nil. This is due to your using animationImages to handle an animation through the view.
You cannot have a UIImageView (or the animation associated to it) used as a placeholder image in SDImage.
You could use:
UIImage *imagety = [UIImage animatedImageWithImages:
[NSArray arrayWithObjects:
[UIImage imageNamed:...],
nil]
duration:10.0f];
(docs)
animatedImageWithImages:duration:
Creates and returns an animated image from an existing set of images.
+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration

Trying to play sound with animation but keep getting "Thread 1 Signal SIGABRT" error in Xcode

My code used to work perfectly when I used to just have an animation. However, now I tried to add sound to the animation also, I've implemented the sound as well as I can however now every time the app runs it shuts down and gives me the SIGABRT error, here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
}
- (IBAction)Button {
AudioServicesPlaySystemSound(SoundID);
}
-(IBAction)startanimating{
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ALambo1.jpg"],
[UIImage imageNamed:#"ALambo2.jpg"],
[UIImage imageNamed:#"ALambo3.jpg"],
[UIImage imageNamed:#"ALambo4.jpg"],
[UIImage imageNamed:#"ALambo5.jpg"],
[UIImage imageNamed:#"ALambo6.jpg"],
[UIImage imageNamed:#"ALambo7.jpg"],
[UIImage imageNamed:#"ALambo8.jpg"],
[UIImage imageNamed:#"ALambo9.jpg"],
[UIImage imageNamed:#"ALambo10.jpg"],
[UIImage imageNamed:#"ALambo11.jpg"],
[UIImage imageNamed:#"ALambo12.jpg"],
[UIImage imageNamed:#"ALambo13.jpg"],
[UIImage imageNamed:#"ALambo14.jpg"],
[UIImage imageNamed:#"ALambo15.jpg"],
[UIImage imageNamed:#"ALambo16.jpg"],
[UIImage imageNamed:#"ALambo17.jpg"],
[UIImage imageNamed:#"ALambo18.jpg"],
[UIImage imageNamed:#"ALambo19.jpg"],
[UIImage imageNamed:#"ALambo20.jpg"],
[UIImage imageNamed:#"ALambo22.jpg"],
[UIImage imageNamed:#"ALambo23.jpg"],
[UIImage imageNamed:#"ALambo24.jpg"],
[UIImage imageNamed:#"ALambo25.jpg"],
[UIImage imageNamed:#"ALambo26.jpg"],
[UIImage imageNamed:#"ALambo52.jpg"],
[UIImage imageNamed:#"ALambo51.jpg"],
[UIImage imageNamed:#"ALambo50.jpg"],
[UIImage imageNamed:#"ALambo49.jpg"],
[UIImage imageNamed:#"ALambo48.jpg"],
[UIImage imageNamed:#"ALambo47.jpg"],
[UIImage imageNamed:#"ALambo46.jpg"],
[UIImage imageNamed:#"ALambo45.jpg"],
[UIImage imageNamed:#"ALambo44.jpg"],
[UIImage imageNamed:#"ALambo43.jpg"],
[UIImage imageNamed:#"ALambo42.jpg"],
[UIImage imageNamed:#"ALambo41.jpg"],
[UIImage imageNamed:#"ALambo40.jpg"],
[UIImage imageNamed:#"ALambo39.jpg"],
[UIImage imageNamed:#"ALambo38.jpg"],
[UIImage imageNamed:#"ALambo37.jpg"],
[UIImage imageNamed:#"ALambo36.jpg"],
[UIImage imageNamed:#"ALambo35.jpg"],
[UIImage imageNamed:#"ALambo34.jpg"],
[UIImage imageNamed:#"ALambo33.jpg"],
[UIImage imageNamed:#"ALambo32.jpg"],
[UIImage imageNamed:#"ALambo31.jpg"],
[UIImage imageNamed:#"ALambo30.jpg"],
[UIImage imageNamed:#"ALambo29.jpg"],
[UIImage imageNamed:#"ALambo28.jpg"], nil];
[animatedimage setAnimationRepeatCount:1];
animatedimage.animationDuration = 1.2;
[animatedimage startAnimating];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"SoundRev" ofType:#"m4a"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Here is the (.h) also:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *animatedimage;
SystemSoundID SoundID;
}
-(IBAction)startanimating;
#end
I haven't used AudioServicesCreateSystemSoundID in quite a while. I suggest switching your code to use AVAudioPlayer instead. That API is both more flexible and easier to use.

Resources