UIImageJPEGRepresentation returns nil
Ive had the problem before with CIImage and to work around that I made a CGImage with the CIImage and then a UIImage with the CGImage.
UIImageJPEGRepresentation and UIImagePNGRepresentation returns nil in iOS 13
This issue is solved in beta 13.1.
UIImageJPEGRepresentation | generateJPEGRepresentation saves image as nil? Swift 3
let image = profilePic.image!.generateJPEGRepresentation()
Check this line, whether it is returning nil value or data? If nil, then use following code to test your image store, it's working. Also ensure your actual image has JPEG file format extension, that you are trying to generate.
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
// For PNG Image
if let image = UIImage(named: "example.png") {
if let data = UIImagePNGRepresentation() {
let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
try? data.write(to: filename)
}
}
For JPG image
if let image = UIImage(named: "example.jpg") {
if let data = UIImageJPEGRepresentation(image, 1.0) {
let filename = getDocumentsDirectory().appendingPathComponent("copy.jpg")
try? data.write(to: filename)
}
}
NSData appendData: UIImageJPEGRepresentation() returns nil
Resolved with:
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"%@.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n", key,key] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:imageData]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
The first newline after my already included data seems to have resolved this
UIImageJPEGRepresentation not working in iOS 13.2.3
After thoroughly examining and researching, it turned out that UIImageJPEGRepresentation with 0.75 compression was causing a crash due to large image size. Changing 0.75 to 0.1 solved the problem.
UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:)
The error is telling you that as of iOS 12 the old UIImageJPEGRepresentation
function has been replaced with the new jpegData
method on UIImage
.
Change:
let imageData = UIImageJPEGRepresentation(image, 0.75)
to:
let imageData = image.jpegData(compressionQuality: 0.75)
Similarly, the use of UIImagePNGRepresentation
has been replaced with pngData()
.
Related Topics
Get the Right Color in iOS7 Translucent Navigation Bar
Xcode 8 Build Crash on iOS 9.2 and Below
Implement Pushkit and Test in Development Behavior
iOS Nsdate() Returns Incorrect Time
Uitableview Checkmark Only One Row at a Time
How to Set iOS Status Bar Background Color in React Native
How to List Out All the Subviews in a Uiviewcontroller in iOS
How to Get Rgb Values from Uicolor
How to Change the Font of the Back Button for My Navigation Bar
Saving Email/Password to Keychain in iOS
iOS 7.0 No Code Signing Identities Found
Choosing the Right iOS Xml Parser
How to Password Protect Writing to Nfc Ntag216 Tag on iOS 13 Using Nfc Core
Issue with Code Autocompletion/Syntax Highlighting in Xcode 4.X
How to Determine If an Nsdate Is Today