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()
.
Use of unresolved identifier 'jpegData'
jpegData
is an instance method of UIImage
, so you need to call it on your UIImage
instance, image
.
guard let data = image.jpegData(compressionQuality: 0.9) else {...
When in doubt about a method name, you should always use the documentation (which is available built into Xcode as well, you can access it using cmd+shift+0
).
Compress Image in iOS 12. How will this code be updated?
Do you mean swift 4.2?
Call it like this yourImageObject.jpegData(compressionQuality: 0.5)
because function changed to public func jpegData(compressionQuality: CGFloat) -> Data?
For more syntax I have a repo you can take a reference with changes from swift 4. https://github.com/alexliubj/Swift-Migration-4.2
Updated:
This API change is from iOS 12, not Swift 4.2. Thanks @rmaddy for your correction.
error loop for UIImageJPEGRepresentation Swift
Found the problem. I had the NotifiationService using Swift 4.2 and the app Swift 4.0. I must have accidentally set it yesterday when checking..
Thanks again guys really silly problem but hey.. we'll now know that this kind of loop error points in different Swift version set in modules . Lesson learned here..
Cheers
Replacing UIImageJPEGRepresentation with UIGraphicsImageRenderer's jpegData
UIImage
can have different orientation depending on camera rotation.
You can dynamically resolve the transformation needed to be applied to the image depending on that orientation, like this:
let renderer = UIGraphicsImageRenderer(size: image.size, format: format)
let imageData = renderer.jpegData(withCompressionQuality: 1, actions: { context in
var workSize = image.size;
workSize.width = floor(workSize.width / image.scale)
workSize.height = floor(workSize.height / image.scale)
// No-op if the orientation is already correct
// if image.imageOrientation == .up { draw image }
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform = CGAffineTransform.identity
switch image.imageOrientation
{
case .down, .downMirrored:
transform = transform.translatedBy(x: workSize.width, y: workSize.height)
transform = transform.rotated(by: CGFloat(Double.pi))
break
case .left, .leftMirrored:
transform = transform.translatedBy(x: workSize.width, y: 0.0)
transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
break
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0.0, y: workSize.height)
transform = transform.rotated(by: CGFloat(-Double.pi / 2.0))
break
case .up, .upMirrored:
break
}
switch image.imageOrientation
{
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: workSize.width, y: 0.0)
transform = transform.scaledBy(x: -1.0, y: 1.0)
break
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: workSize.height, y: 0.0);
transform = transform.scaledBy(x: -1.0, y: 1.0);
break
case .up, .down, .left, .right:
break
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let ctx = context.cgContext
ctx.concatenate(transform)
switch image.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
ctx.draw(image.cgImage!, in: CGRect(x: 0.0, y:0.0, width: workSize.height, height: workSize.width))
break;
default:
ctx.draw(image.cgImage!, in: CGRect(origin: .zero, size: workSize))
break;
}
})
Answer based on UIImage+fixOrientation
Related Topics
How to Use Uiviewcontrolleranimatedtransitioning with Uinavigationcontroller
iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C
Uirefreshcontrol Stuck After Switching Tabs in Uitabbarcontroller
How to Disable User Interaction on Mkmapview
Uiwindow with Wrong Size When Using Landscape Orientation
Apple Watch, Watchkit Extension and Main Application
Xcode 8 Swift 3 Uitableview Space Between Cells
How to Add a Credit/Debit Card into Apple Wallet from the iOS App
How to Play Movie with a Url Using a Custom Nsurlprotocol
Is There Any Limitation to Distribution of Apps in iOS Enterprise Program
Customize Navigation Bar with Title View
Swift: Natively Detect If App Has Crashed
#Import <Libxml/Tree.H> File Not Found After Xcode Update
Apple MACh-O Linker & Ditto Error - Xcode 8
Get Current View Controller from the App Delegate (Modal Is Possible)