[英]Huge CPU usage when calling 'UIImagePickerController didFinishPickingMediaWithInfo' iOS7 XCode
I noticed a huge CPU usage when I press 'Use Photo' on UIImagePickerController calling the proper method: 当我在UIImagePickerController上按“使用照片”并调用正确的方法时,我发现CPU使用率很高:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
}
even if the method is empy! 即使方法是空的! Here an image logging CPU usage while above method is called. 在此调用上述方法时的图像日志记录CPU使用率。
Top that I recorded was 169%. 我记录的最高成绩是169%。 So wtf?!? 那么wtf?!?
This is because size of the image that you are using is very large normally more than 2mb. 这是因为您使用的图像大小通常很大,通常大于2mb。 So so a image resize before using it. 因此,在使用前请调整图像大小。 here is the code snippet: 这是代码片段:
- (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
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.