将UIImage转换为NSData

我在我的应用程序中使用这个代码,这将帮助我发送图像。

但是,我有一个图像的图像视图。 我没有档案,但有我的形象在我身边。 我如何更改下面的代码? 谁能告诉我如何将myimage转换为NSData

 // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"]; 

根据您的图像格式,尝试以下方法之一:

UIImageJPEGRepresentation

以JPEG格式返回指定图像的数据。

 NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality ); 

UIImagePNGRepresentation

以PNG格式返回指定图像的数据

 NSData * UIImagePNGRepresentation ( UIImage *image ); 

这里的文档 。

编辑:

如果你想访问组成UIImage的原始字节,你可以使用这种方法:

 CGDataProviderRef provider = CGImageGetDataProvider(image.CGImage); NSData* data = (id)CFBridgingRelease(CGDataProviderCopyData(provider)); const uint8_t* bytes = [data bytes]; 

这将给你图像RGB像素的低级表示。 (如果你不使用ARC,省略CFBridgingRelease位)。

 NSData *imageData = UIImagePNGRepresentation(myImage.image); 

创build图像的参考….

 UIImage *rainyImage = [UIImage imageNamed:@"rainy.jpg"]; 

在图像视图中显示图像… imagedisplay是imageview的参考:

 imagedisplay.image = rainyImage; 

通过传递UIImage引用将其转换为NSData ,并以浮点值提供压缩质量:

 NSData *imgData = UIImageJPEGRepresentation(rainyImage, 0.9); 

如果您在UIImageView中有一个图像,例如“myImageView”,您可以执行以下操作:

使用UIImageJPEGRepresentation()或UIImagePNGRepresentation()像这样转换你的图像:

 NSData *data = UIImagePNGRepresentation(myImageView.image); //or NSData *data = UIImageJPEGRepresentation(myImageView.image, 0.8); //The float param (0.8 in this example) is the compression quality //expressed as a value from 0.0 to 1.0, where 1.0 represents //the least compression (or best quality). 

你也可以把这段代码放到一个GCD块中,并在另一个线程中执行,在这个过程中显示一个UIActivityIndi​​catorView。

 //*code to show a loading view here* dispatch_queue_t myQueue = dispatch_queue_create("com.my.queue", DISPATCH_QUEUE_SERIAL); dispatch_async(myQueue, ^{ NSData *data = UIImagePNGRepresentation(myImageView.image); //some code.... dispatch_async( dispatch_get_main_queue(), ^{ //*code to hide the loading view here* }); }); 
 - (void) imageConvert { UIImage *snapshot = self.myImageView.image; [self encodeImageToBase64String:snapshot]; } call this method for image convert in base 64 -(NSString *)encodeImageToBase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; } 

在Swift中的解决scheme4

 extension UIImage { var data : Data? { return cgImage?.dataProvider?.data as Data? } } 

使用带数据的if-let块来防止应用程序崩溃和安全执行代码,因为函数UIImagePNGRepresentation返回一个可选值。

 if let img = UIImage(named: "TestImage.png") { if let data:Data = UIImagePNGRepresentation(img) { // Handle operations with data here... } } 

注意: 数据是Swift 3类。 Swift 3使用数据而不是NSData

通用图像操作(如png和jpg):

 if let img = UIImage(named: "TestImage.png") { //UIImage(named: "TestImage.jpg") if let data:Data = UIImagePNGRepresentation(img) { handleOperationWithData(data: data) } else if let data:Data = UIImageJPEGRepresentation(img, 1.0) { handleOperationWithData(data: data) } } ******* func handleOperationWithData(data: Data) { // Handle operations with data here... if let image = UIImage(data: data) { // Use image... } } 

通过使用扩展:

 extension UIImage { var pngRepresentationData: Data? { return UIImagePNGRepresentation(img) } var jpegRepresentationData: Data? { return UIImageJPEGRepresentation(self, 1.0) } } ******* if let img = UIImage(named: "TestImage.png") { //UIImage(named: "TestImage.jpg") if let data = img.pngRepresentationData { handleOperationWithData(data: data) } else if let data = img.jpegRepresentationData { handleOperationWithData(data: data) } } ******* func handleOperationWithData(data: Data) { // Handle operations with data here... if let image = UIImage(data: data) { // Use image... } }