2013-11-03 55 views
1

時,我注意到,當我按下的UIImagePickerController「使用照片」調用的正確方法了巨大的CPU佔用率:巨大的CPU使用率調用「的UIImagePickerController didFinishPickingMediaWithInfo」 iOS7的XCode

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

} 

即使該方法empy!這裏調用上述方法時的圖像記錄CPU使用情況。

image http://i39.tinypic.com/osu1pl.png

頂一下,我錄得169%。所以wtf?!?

回答

0

這是因爲您使用的圖像的大小通常非常大,通常超過2MB。因此,在使用它之前調整圖像大小。 這裏是代碼片段:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    if([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) { 
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage]; 

    if (!originalImage) 
     return; 

    // Optionally set a placeholder image here while resizing happens in background 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Set desired maximum height and calculate width 
     CGFloat height = 640.0f; // or whatever you need 
     CGFloat width = (height/self.view.frame.size.height) * self.view.frame.size.width; 

     // Resize the image 
     UIImage * image = [originalImage resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationDefault]; 

     //save/use the image here 
     UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);//for saving to camera roll 
    }); 
} 
} 
相關問題