I think you need to resize image because it is showing error that image size will be not mor then 16 MB, use this extension in order to resize image with quality
let newImage = image?.resizeImage(targetSize: CGSize(width: 400, height: 400))
let imageData = newImage?.jpeg(.lowest) // send this image to UIPasteboard.general.image
Thread 2: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit=16 MB, unused=0x0)
extension UIImage {
func resizeImage(targetSize: CGSize) -> UIImage? {
let size = self.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
let newSize = widthRatio > heightRatio ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage?
}
}
public 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 self.jpegData(compressionQuality: jpegQuality.rawValue)
}
}