【问题标题】:Data not being written to file?数据未写入文件?
【发布时间】:2015-07-21 09:16:33
【问题描述】:

我使用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];
}

...
    [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;
...

我稍后尝试检索图片以在表格视图单元格中使用,但没有显示任何内容。所以我开始尝试跟踪哪里出了问题,显然数据实际上并没有写入文件路径指定的文件。我从战略性放置的NSLogs 获得以下控制台读数:

pngData 被写入 /var/mobile/Containers/Data/Application/114FC402-83E0-4D15-B1E0-68E09DAB34DC/Documents1431292149 的文件
这个文件的大小应该是213633

这从文件路径返回:

fileSizeFromFilePath 的大小为 0

我查看了很多 SO 问题,但无法弄清楚。有人可以告诉我哪里出错了吗?

【问题讨论】:

    标签: ios objective-c filepath nsdocumentdirectory


    【解决方案1】:

    pngData 写入不存在的错误路径/path/to/sandbox/Documents1431292149

    你应该替换

    filePath = [documentsPath stringByAppendingString:timeTag];
    

    filePath = [documentsPath stringByAppendingPathComponent:timeTag];
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2016-03-06
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2023-02-24
      • 2021-07-18
      • 1970-01-01
      • 2012-09-08
      相关资源
      最近更新 更多