gpt4 book ai didi

ios - 从 UIImagePickerController 调整 UIImage 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:23 26 4
gpt4 key购买 nike

我目前正在使用这里的代码拍照

- (void) cameraButtonSelected
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];
}

我允许用户编辑照片,但是当我出于某种原因使用此委托(delegate)方法时,在用户按下“使用照片”后,UIImagePickerController 不会从 View 中移除

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

我想知道

  1. 如何在按下“使用照片”按钮后从 View 中删除 UIImagePickerController
  2. 如何调整刚刚拍摄的照片的大小,因为我需要将较小的变体发送到我的服务器

最佳答案

您可以通过在信息字典中查询 UIImagePickerControllerEditedImage 来获取图像。要从 View 中删除 ImagePicker,只需关闭选择器即可。这是调整大小的代码。只需用您的图像实例调用它

您可以使用此功能将图像缩放到特定尺寸

- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

所以你的最终代码应该是这样的

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {    
[picker dismissViewControllerAnimated:YES completion:Nil];
UIImage *image = info[UIImagePickerControllerEditedImage];
image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size
}

关于ios - 从 UIImagePickerController 调整 UIImage 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com