问题描述
我在使用CPU时遇到问题.当我启动我的应用程序时,其动画开始播放的速度很快,突然的动画速度降低了,然后崩溃了.但是,当我使用Activity Monitor(Instrument)检查该应用程序时,我的应用程序使用的CPU大约占80%-90%.我无法降低CPU使用率.
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.
代码:
-(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.
谢谢
推荐答案
-(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]];
}
做类似上面的事情.您可以在其中移动imageview的位置.
Do some thing like the above. Where you can move the imageview.
也可以使用另一个按钮调用合并,您可以在其中随意移动图像,但是当需要合并时,请单击一个按钮.这样,您将只调用一次Merge,并且不会对CPU造成任何负担.
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
- 实例变量
- 属性
- 手势识别器等
这篇关于降低应用程序的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!