NSData 到 UIImage
我正在尝试保存 UIIMage,然后检索它并显示它。我成功地将图像保存在捆绑包中,从那里检索它并显示。我的问题来自于当我尝试将图像保存到磁盘(将其转换为 NSData),然后检索它时。我像这样将图像转换为 NSData...
NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);
然后我像这样将其写入磁盘...
[topImageData writeToFile:topPathToFile atomically:NO];
然后我尝试像这样检索它...
topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];
它不返回图像(大小为零)。所以后来我尝试了……
NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];
但没有成功。当我单步执行调试器时,我确实看到数据包含正确的字节数,但我对为什么没有创建我的图像感到困惑。我是不是少了一步?在转换为图像之前我需要对 NSData 执行某些操作吗?
I'm trying to save a UIIMage and then retrieve it and display it. I've been successful by saving an image in the bundle, retrieving it from there and displaying. My problem comes from when I try to save an image to disk (converting it to NSData), then retrieving it. I convert the image to NSData like so...
NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);
then I write it to disk like so...
[topImageData writeToFile:topPathToFile atomically:NO];
then I tried to retrieve it like so...
topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];
which returns no image (the size is zero). so then I tried...
NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];
to no avail. When I step through the debugger I do see that data contains the correct number of bytes but I'm confused as to why my image is not being created. Am I missing a step? Do I need to do something with NSData before converting to an image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个代码。这对我有用。
保存数据。
创建路径来保存图像。
将图像转换为数据对象并将其保存到文件中。
检索保存的图像
创建一个图像视图并使用检索到的图像加载它。
Try this code. This worked for me.
Saving the data.
create path to save the image.
convert the image to a Data object and save it to the file.
retrieving the saved image
Create an imageview and load it with the retrieved image.
您应该能够使用 UIImage 的类方法
或者
这不起作用吗?
希望这有帮助!
You should be able to use class methods of UIImage
OR
Did that not work?
Hope this helps!
以防万一这对某人有帮助,从 iOS6 开始,我们有 imageWithData:scale 方法。要从 NSData 对象获取具有正确比例的 UIImage,请使用该方法,声明用于存储原始图像的比例。例如:
这里,myimage是存储原始图像的NSData对象。在示例中,我使用了屏幕的比例。如果您对原始图像使用其他比例,请改用该值。
Just in case this helps someone, from iOS6 we have the imageWithData:scale method. To get an UIImage with the right scale from an NSData object, use that method, declaring the scale you use to store the original image. For example:
Here, myimage is the NSData object where you stored the original image. In the example, I used the scale of the screen. If you use another scale for the original image, use that value instead.
看看这个: http://www.nixwire.com/获取 uiimage-to-work-with-nscoding-encodewithcoder/。
它正是我认为解决您问题的方法。你不能仅仅用 NSData 制作图像,即使这是常识所暗示的。您必须使用 UIImagePNGRepresentation。让我知道这是否有帮助。
Check this out: http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/.
It has exactly what I think the solution to your problem is. You can't just make an image out of NSData, even though that's what common sense suggests. You have to use UIImagePNGRepresentation. Let me know if this helps.
将 if-let 块与数据一起使用以防止应用程序崩溃安全执行代码,因为函数 UIImagePNGRepresentation 返回一个可选值。
通用图像操作(如 png 和 jpg 两者):
通过使用扩展:
Use if-let block with Data to prevent app crash & safe execution of code, as function UIImagePNGRepresentation returns an optional value.
Generic image operations (like png & jpg both):
By using extension: