简体   繁体   English

iOS:如何处理图像?

[英]iOS : How to handle images?

I'm making a photo managing app. 我正在制作照片管理应用。 I have created a class called photo, which has the name of the photo, notes on the photo, and the image. 我创建了一个名为photo的类,它有照片的名称,照片上的注释和图像。 I know how to set the name and notes - they're both NSStrings, but what data type should I use for my image? 我知道如何设置名称和注释 - 它们都是NSStrings,但我应该为我的图像使用什么数据类型?

At the moment, I'm using NSData, is that correct? 目前,我正在使用NSData,这是正确的吗?


Edit 编辑

Apparently, NSData is right. 显然,NSData是对的。 So, how should I get an image from a UIImage into the NSData object and back again? 那么,我应该如何将UIImage中的图像转换为NSData对象并再次返回?

Data from UIImage: 来自UIImage的数据:

NSData *imageData = UIImagePNGRepresentation(image);

Alternatively, UIImage from data: 或者,来自数据的UIImage:

UIImage *image = [UIImage imageWithData:imageData];

You can store your image as an UIImage or NSData 您可以将图像存储为UIImage或NSData

You can simply get one from another: 你可以简单地从另一个:

NSData *data = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:data];

In case you need the image for presenting it's better practice to hold an UIImage instance in youre object transferring it to/from NSData when you need to save it or get it either from the net or the disk. 如果您需要图像来呈现它,那么当您需要保存或从网络或磁盘获取UIImage实例时,将对象传输到NSData或从NSData传输它是更好的做法。 If youre case is more like file manager then photo viewer, then this answer is not aplicable. 如果你的情况更像文件管理器然后照片查看器,那么这个答案是不适用的。 Anyway the transfering routines were provided to you by previous answer. 无论如何,转移程序是通过以前的答案提供给你的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM