Lower the CPU usage of an app

拟墨画扇 提交于 2019-12-24 20:34:55

问题


I am having an issue with CPU usage. When I starts my app its animation starts at good speed and all of sudden animation speed gets lower down and then app gets crashed. But when I checked the app with Activity Monitor(Instrument) my app use CPU near about 80%-90%. I am unable reduce the CPU usage.

CODE:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   

    CGPoint location;

    UITouch *touch = [[event allTouches] anyObject];
    location = [touch locationInView:self.view];



}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{

    CGPoint location;

    UITouch *touch = [[event allTouches] anyObject];
    location = [touch locationInView:self.view];

    touchMovedX = location.x;
    touchMovedY = location.y; 

   [self merge];
}  





 // -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    //{
       // [self merge];
   // }


-(void)merge
{

    CGSize size = CGSizeMake(320, 480);
    UIGraphicsBeginImageContext(size);

    CGPoint point1 = CGPointMake(0,0);

    CGPoint point2 = CGPointMake(touchMovedX,touchMovedY);

    UIImage *imageOne = [UIImage imageNamed:@"iphone.png"];
    [imageOne drawAtPoint:point1];

    UIImage *imageTwo = [UIImage imageNamed:@"Cloud.png"];

    [imageTwo drawAtPoint:point2];

    imageB.image=imageTwo;

    UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    imageview.image=imageC;

    [self.view addSubview:imageview];
}

-(IBAction)save:(id)sender  {

UIImage* imageToSave = imageview.image;

UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
[imageToSave release];

}

Any help would be appreciable.

Thanks


回答1:


-(void)viewDidAppear:(BOOL)animated{
    UIPanGestureRecognizer *pgr1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(image1Moved:)];
    [iv1 addGestureRecognizer:pgr1];

}

-(void)image1Moved:(UIPanGestureRecognizer *)pgr{
    NSLog(@"Came here");
     [iv1 setCenter:[pgr locationInView:self.view]];
}

Do some thing like the above. Where you can move the imageview.

Also, call the merge using another button, where you move the image at will, but when it is time to merge, click a button. That way, you will call Merge only once and it will not be any load on the CPU.

Looks like you are a beginner and I would highly recommend that you follow some tutorials and learn some more about

  1. Instance Variables
  2. Properties
  3. gesture recognizers etc



回答2:


Don't call touchesMoved in your touchesBegan code. touchesMoved is called by iOS in response to touches being moved

Likewise with touchesEnded - this gets called when the user removes the finger from the screen

In addition - your code for merge is adding more and more subviews to your view - at the end of every call to merge you are callin [self.view addSubview:imageview] which is going increase your CPU usage in handling all the subviews. Everytime you move your finger in touches moved then this will be adding a new subview and never removing them.




回答3:


It is not touchesMoved or touchesBegan which is causing the CPU usage. It is definitely [self merge]. I am assuming that you are doing some CPU intensive job in [self merge] and that is being done so many times.

You have to do any CPU intensive job on another thread for the app to be responsive. Also if you are doing stuff at every move, then it may become too slow.

Please post the code for what you are doing in merge method.

There are three things which you can do

  1. Improve the merge method to make it efficient.

  2. Use Grand Central dispatch

Read up about dispatch_async(dispatch_get_global_queue, ^{ }); etc. This will be under Grand Central Dispatch section.

  1. Another way is to do [self performSelector:@(merge) afterDelay:0.5s]; This method will call merge, only once every 0.5 seconds.

and then if you do not want the same method to be called so many times, or it is not necessary for every move, just before that call

[NSObject cancelPreviousPerformRequestsWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#>

The cancel method will cancel previous invocations and call the method again.

But again, it all depends on what you are trying to do.



来源:https://stackoverflow.com/questions/13761049/lower-the-cpu-usage-of-an-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!