How to compress of reduce the size of an image before uploading to Parse as PFFile? (Swift)
Yes you can use UIImageJPEGRepresentation
instead of UIImagePNGRepresentation
to reduce your image file size. You can just create an extension UIImage as follow:
Xcode 8.2 • Swift 3.0.2
extension UIImage {
enum JPEGQuality: CGFloat {
case lowest = 0
case low = 0.25
case medium = 0.5
case high = 0.75
case highest = 1
}
/// Returns the data for the specified image in JPEG format.
/// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.
/// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
func jpeg(_ quality: JPEGQuality) -> Data? {
return UIImageJPEGRepresentation(self, quality.rawValue)
}
}
edit/update:
Xcode 10 Swift 4.2
extension UIImage {
enum JPEGQuality: CGFloat {
case lowest = 0
case low = 0.25
case medium = 0.5
case high = 0.75
case highest = 1
}
/// Returns the data for the specified image in JPEG format.
/// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.
/// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
func jpeg(_ jpegQuality: JPEGQuality) -> Data? {
return jpegData(compressionQuality: jpegQuality.rawValue)
}
}
Usage:
if let imageData = image.jpeg(.lowest) {
print(imageData.count)
}
How to compress an image via Javascript in the browser?
In short:
- Read the files using the HTML5 FileReader API with .readAsArrayBuffer
- Create a Blob with the file data and get its url with window.URL.createObjectURL(blob)
- Create new Image element and set it's src to the file blob url
- Send the image to the canvas. The canvas size is set to desired output size
- Get the scaled-down data back from canvas via canvas.toDataURL("image/jpeg",0.7) (set your own output format and quality)
- Attach new hidden inputs to the original form and transfer the dataURI images basically as normal text
- On backend, read the dataURI, decode from Base64, and save it
Source: code.
Compress image size using Java
Unfortunately the lossy JPEG compression compresses far better than the lossless PNG compression. You could restrict width and height of the image and scale proportionally.
So I would switch to JPEG and restrict the size.
How to compress the image size using .NET Core?
I have used Magick.NET library which is available for both .NET and .NET Core to compress images. Refer to its documentation for more details.
In your case you either need to save the image temporarily somewhere and use the path (similar to my example), or convert it to an array of byte
in memory and read that since it accept Byte[]
using (MagickImage image = new MagickImage(@"YourImage.jpg"))
{
image.Format = image.Format; // Get or Set the format of the image.
image.Resize(40, 40); // fit the image into the requested width and height.
image.Quality = 10; // This is the Compression level.
image.Write("YourFinalImage.jpg");
}
Related Topics
How to Use Assert on Android Devices
Changing Position of the Dialog on Screen Android
How to Send Http Post Request and Receive Response
How to Know the Map Is Ready to Get Used When Using the Supportmapfragment
How to Get an Android Wakelock to Work
React Native Android Fetch Failing on Connection to Local API
System.Loadlibrary(...) Couldn't Find Native Library in My Case
Gradle Build Tool Cannot Find Play-Services-Tasks.Aar? Why
Dynamic Form with Repeating Form
Android: Scrolling an Imageview
Android Webview Style Background-Color:Transparent Ignored on Android 2.2
How to Make Gradient Background in Android
Why Do I Get Access Denied to Data Folder When Using Adb
How to Use Image as Button in Ionic
Error "Could Not Get Batchedbridge, Make Sure Your Bundle Is Packaged Properly" on Start of App