Uiimagejpegrepresentation Returns Nil

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



Leave a reply



Submit