I'm using FastttCamera as a wrapper on AVFoundation in order to implement a camera in my app. Things appear to work fine until I try to save a captured image with the following code:
我正在使用FastttCamera作为AVFoundation的包装器,以便在我的应用程序中实现相机。在我尝试使用以下代码保存捕获的图像之前,事情似乎正常工作:
- (void)cameraController:(FastttCamera *)cameraController didFinishScalingCapturedImage:(FastttCapturedImage *)capturedImage
{
//Use the image's data that is received
pngData = UIImagePNGRepresentation(capturedImage.scaledImage);
NSLog(@"1 The size of pngData should be %lu",(unsigned long)pngData.length);
//Save the image someplace, and add the path to this transaction's picPath attribute
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *timeTag = [NSString stringWithFormat:@"%d",timestamp];
filePath = [documentsPath stringByAppendingString:timeTag]; //Add the file name
NSLog(@"The picture was saved at %@",filePath);
[self.imageWell setImage:capturedImage.scaledImage];
}
and
...
[pngData writeToFile:filePath atomically:YES]; //Write the file
NSLog(@"The pngData is written to file at %@",filePath);
NSLog(@"2 The size of this file should be %lu",(unsigned long)pngData.length);
self.thisTransaction.picPath = filePath;
...
I try to retrieve the picture later for use in a tableview cell, but nothing shows up. So I started trying to track where things go wrong, and apparently the data is not actually being written to the file specified by the file path. I'm getting the following console readouts from strategically placed NSLog
s:
我稍后尝试检索图片以用于tableview单元格,但没有显示任何内容。所以我开始尝试跟踪出错的地方,显然数据实际上并没有写入文件路径指定的文件。我从策略性的NSLogs获得以下控制台读数:
The pngData is written to file at /var/mobile/Containers/Data/Application/114FC402-83E0-4D15-B1E0-68E09DAB34DC/Documents1431292149
The size of this file should be 213633pngData写入文件/ var / mobile / Containers / Data / Application / 114FC402-83E0-4D15-B1E0-68E09DAB34DC / Documents1431292149此文件的大小应为213633
and this return from the file at the file path:
并从文件路径的文件返回:
The size of fileSizeFromFilePath is 0
fileSizeFromFilePath的大小为0
I've looked at quite a few SO questions, but haven't been able to figure it out. Can someone please show me where I'm going wrong?
我已经看了很多SO问题,但还是没能弄清楚。有人可以告诉我哪里出错了吗?
1 个解决方案
#1
The pngData is written to a incorrect path /path/to/sandbox/Documents1431292149
which is not exist.
pngData被写入不正确的路径/ path / to / sandbox / Documents1431292149,这是不存在的。
You should replace
你应该更换
filePath = [documentsPath stringByAppendingString:timeTag];
with
filePath = [documentsPath stringByAppendingPathComponent:timeTag];
#1
The pngData is written to a incorrect path /path/to/sandbox/Documents1431292149
which is not exist.
pngData被写入不正确的路径/ path / to / sandbox / Documents1431292149,这是不存在的。
You should replace
你应该更换
filePath = [documentsPath stringByAppendingString:timeTag];
with
filePath = [documentsPath stringByAppendingPathComponent:timeTag];