当我在 UIImagePickerController 上按“使用照片”调用正确的方法时,我注意到 CPU 使用率很高:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
}
即使方法是空的!此处调用上述方法时记录 CPU 使用情况的图像。
我记录的最高值为 169%。所以wtf?!?
当我在 UIImagePickerController 上按“使用照片”调用正确的方法时,我注意到 CPU 使用率很高:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
}
即使方法是空的!此处调用上述方法时记录 CPU 使用情况的图像。
我记录的最高值为 169%。所以wtf?!?
这是因为您使用的图像尺寸非常大,通常超过 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
});
}
}