objective-c - uiimage保存到本地 - uiimage转nsdata




将UIImage转换为NSData (5)

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

但是,我有一个图像的图像视图。 我没有档案,但有我的形象。 我如何更改下面的代码? 任何人都可以告诉我如何将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"];

在Swift中的解决方案4

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...
     }
}

如果您在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*
    });
});

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

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);




nsdata