gpt4 book ai didi

ios - 来自文件的 UIImage 始终为零

转载 作者:行者123 更新时间:2023-11-29 01:28:00 26 4
gpt4 key购买 nike

我正在制作一个文档扫描应用

扫描的文档存储在应用程序结构中的临时文件中,该文件的路径存储在 NSString 变量中

此图像被传递到成功加载的 UIImageView

[self.cameraViewController captureImageWithCompletionHander:^(NSString *imageFilePath)
{
UIImageView *captureImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imageFilePath]];
captureImageView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
captureImageView.frame = CGRectOffset(weakSelf.view.bounds, 0, -weakSelf.view.bounds.size.height);
captureImageView.alpha = 1.0;
captureImageView.contentMode = UIViewContentModeScaleAspectFit;
captureImageView.userInteractionEnabled = YES;
[weakSelf.view addSubview:captureImageView];

然后想把图片转成base64准备上传。为了做到这一点,我将调用以下接受 UIImage 对象的对象

- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

imageFilePath变量的值为

/private/var/mobile/Containers/Data/Application/79C28B96-275D-48F1-B701-CABD699C388D/tmp/ipdf_img_1447880304.jpeg

我用这个从文件中获取图像

UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageFilePath ofType:nil]];

base64String = [self encodeToBase64String:img];

问题在于 UIImage 对象始终为 nill(或 nill)。起初我认为问题可能出在路径上,但图像加载在 UIImageView 中。

谁能帮我弄清楚为什么这不会返回存储在 imageFilePath 变量中的图像

最佳答案

替换:

UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageFilePath ofType:nil]];

与:

UIImage *img = [UIImage imageWithContentsOfFile:imageFilePath];

您没有从 bundle 中加载图像。

此外,请确保您没有尝试在应用程序的执行过程中使用完整路径。仅保留相对路径,因为应用程序沙箱的路径可能会随着时间的推移而改变。

关于ios - 来自文件的 UIImage 始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33790791/

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