我正在将一个 .jpg 文件写入我的应用程序的 Documents 目录,如下所示:
NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0);
BOOL retValue = [img writeToFile:myFilePath atomically:YES];
后来,我将该图像加载回 UIImage 使用:
UIImage *myImage = [UIImage imageWithContentsOfFile:path];
我知道它有效,因为我可以在表格单元格中绘制图像,这很好。现在,如果我尝试使用 UIImageJPEGRepresentation(myImage, 1.0),调试器会打印出以下几行:
<Error>: Not a JPEG file: starts with 0xff 0xd9
<Error>: Application transferred too few scanlines
该函数返回 nil。有谁知道为什么会发生这种情况?UIImage 数据加载后我没有做任何操作。我只是将 UIImage 提供给单元格中的图像视图。我设置了图像视图属性,使单元格中的所有图像都排成一行并且大小相同,但我认为这与将 UIImage 转换为 NSData 没有任何关系。