2012-12-07 65 views
0

我遇到CPU使用率問題。當我開始我的應用程序時,它的動畫以良好的速度開始,所有突然的動畫速度降低,然後應用程序崩潰。但是當我使用Activity Monitor(Instrument)檢查應用程序時,我的應用程序使用的CPU接近80%-90%。我無法減少CPU使用率。降低應用程序的CPU使用率

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]; 

}

任何幫助將是明顯的。

感謝

+1

請出示您用於動畫的代碼。 – sosborn

+0

@sosborn檢查...我已分享代碼 –

回答

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]]; 
} 

做一些像上面這樣的事情。你可以在哪裏移動imageview。

另外,使用另一個按鈕調用合併,您可以隨意移動圖像,但是在合併時,請單擊按鈕。這樣,您只需調用一次Merge,並且不會對CPU造成任何負載。

看起來,你是一個初學者,我會強烈建議您遵循一些教程和多學習一些有關

  1. 實例變量
  2. 屬性
  3. 手勢識別等
2

不要在touchesBegan代碼調用touchesMovedtouchesMoved由iOS的響應打電話來觸摸移動

同樣與touchesEnded - 這被稱爲當用戶從屏幕移開

除了手指 - 你對合並的代碼添加越來越多的子視圖您查看 - 在每次調用合併結束時,您都會呼叫[self.view addSubview:imageview]這會增加處理所有子視圖的CPU使用率。每次你移動你的手指觸摸移動然後這將添加一個新的子視圖,永遠不會刪除它們。

+0

嗨,感謝您的支持...我已經對我的代碼做了一些改動,我已經刪除了** - (void)touchesEnded:(NSSet *)touch withEvent :(UIEvent *)事件**,並在移動委託中調用了我的合併,並且它仍然顯示CPU使用率負載,應用程序崩潰。 –

+1

@DeepakKhiwani查看我的更新回答。你應該創建一個你放在那裏的'UIImageView',然後更新它裏面的圖像,而不是每次都添加一個新的'UIImageView' –

0

它不是touchesMoved或touchesBegan這是造成CPU使用率。這絕對是[自我併合]。我假設你正在[自我合併]中完成一些CPU密集型工作,並且這麼做了很多次。

您必須在另一個線程上執行任何CPU密集型工作才能讓應用程序響應。此外,如果你一舉一動,那麼它可能會變得太慢。

請發佈您在合併方法中所做的代碼。

有三件事情,你可以做

  1. 提高合併方法,使之高效。

  2. 使用大中央調度

閱讀了有關dispatch_async(dispatch_get_global_queue,^ {});這將在Grand Central Dispatch部分下進行。

  1. 另一種方法是[self performSelector:@(merge)afterDelay:0.5s]; 此方法將調用合併,每0.5秒只調用一次。

,然後如果你不希望同樣的方法被調用了很多次,還是沒有必要的一舉一動,只是通話

[NSObject的cancelPreviousPerformRequestsWithTarget前:<#(ID)# >選擇器:<#(SEL)#> object:<#(id)#>

取消方法將取消先前的調用並再次調用該方法。

但是,這一切都取決於你想要做什麼。

+0

我也強烈建議你使用手勢識別器,因爲即使是專家也可以使用觸摸級別apis很難找到。 – Srikanth

+0

真的很高興看到你對我的請求的答案......以及我已經發布的代碼,我正在試圖做...請看看,讓我知道是否有任何一種錯誤。謝謝 –