gpt4 book ai didi

ios - UIImagePickerControllerEditedImage 得到零

转载 作者:行者123 更新时间:2023-11-29 03:25:57 25 4
gpt4 key购买 nike

大家好,我正在使用 UIImagePickerController 进行一些图像编辑。这是 imagePickerController:didFinishPickingMediaWithInfo 中的一些代码:

    UIImage *editedImg = [info objectForKey:UIImagePickerControllerEditedImage];
UIImageView *imgView = [[UIImageView alloc] initWithImage:editedImg];
CGRect imgFrm = imgView.frame;
float rate = imgFrm.size.height / imgFrm.size.width;
imgFrm.size.width = size;
imgFrm.size.height = size * rate;
imgFrm.origin.x = 0;
imgFrm.origin.y = (size - imgFrm.size.height) / 2;
[imgView setFrame:imgFrm];

UIView *cropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
[cropView setBackgroundColor:[UIColor blackColor]];
[cropView addSubview:imgView];

UIImage *croppedImg = [MyUtil createUIImageFromUIView:cropView];

上面是在size*size的 View 中设置图像,当picker返回的图像高度小于size时,从 View 中绘制图像。

这是 createUIImageFromUIView:(UIView*)view 的代码:

+ (UIImage *)createUIImageFromUIView:(UIView *)view
{

UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 2.0);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return viewImage;
}

我的问题是:调试时,'editedImg'(定义在第一行)只显示'nil'。但是,下面的代码运行良好。我正确地获取了 corpView(也显示为“nil”)并得到了裁剪后的图像,并且可以将其编码为 base64 编码的字符串以发送到服务器端。 我只是想知道为什么editedImg是nil(由[info objectForKey:UIImagePickerControllerEditedImage]返回,但是当我选择在 Debug模式下打印信息时,控制台中的输出不是nil)?

最佳答案

editdImg 得到 nil,试试:

UIImage *editedImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

获取文件大小:

- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}

祝你好运!

关于ios - UIImagePickerControllerEditedImage 得到零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464415/

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