I have these functions that I've cobbled together to resize and save an image. But it doesn't seem to be resizing my images properly -- a 150x150 image attempted to be resized as 50x50 image ends up saved as 100x100. Any ideas what's causing it?
extension NSImage {
#discardableResult
func saveAsPNG(url: URL) -> Bool {
guard let tiffData = self.tiffRepresentation else {
print("failed to get tiffRepresentation. url: \(url)")
return false
}
let imageRep = NSBitmapImageRep(data: tiffData)
guard let imageData = imageRep?.representation(using: .PNG, properties: [:]) else {
print("failed to get PNG representation. url: \(url)")
return false
}
do {
try imageData.write(to: url)
return true
} catch {
print("failed to write to disk. url: \(url)")
return false
}
}
}
enum error:Error {
case imageCreationFailure
}
func resizeImageByFactor(_ url:URL) throws {
let image = NSImage(byReferencing: url)
guard image.isValid else { throw error.imageCreationFailure }
let reSize = NSSize(width: 50, height: 50)
let oldRect = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height)
let newRect = CGRect(x: 0.0, y: 0.0, width: reSize.width, height: reSize.height)
let newImage = NSImage(size: reSize)
newImage.lockFocus()
image.draw(in: newRect, from: oldRect, operation: .copy, fraction: 1.0)
newImage.unlockFocus()
newImage.size
let url = URL(fileURLWithPath: "test.jpg", relativeTo: url.deletingLastPathComponent())
newImage.saveAsPNG(url: url)
}
OS X & iOS devices have scaling factors. The iPhone 5, 5S, 6, etc. all have a scaling factor of 2x. The iPhone 6 Plus has a scaling factor of 3x. The old non-retina iPhones have a 1x scaling factor. My OS X machine with a 4K display has a scaling factor of 2x.
What you should do is this:
let scalingFactor = NSScreen.mainScreen()?.backingScaleFactor;
let size = NSSize(width: 50 / scalingFactor!, height: 50 / scalingFactor!);
Related
I'm trying to save a scaled UUImage in default Document folder in the SwiftUI world. In particular I would like to resize the image before saving it. The flow is: the user takes a picture and at some point this picture is saved. Regardless of its size, its width should be 512 px and its height will be calculated keeping the aspect ratio. I wrote a function to resize the picture before save it and if I debug, the original picture's size is changed correctly but after I save it in document folder, the width and height are completely different. What am i doing wrong?
this is my function that resizes the image:
extension UIImage {
func imageResized(to size: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { _ in
draw(in: CGRect(origin: .zero, size: size))
}
}
}
this is my function that resize and save the picture:
func saveImageToDocuments(imageName: String, image: UIImage) -> String? {
guard let directoryToSave = getOrCreateCurrentMonthYearFolder() else { return nil }
let absolutePath = directoryToSave.absoluteString
let tokens = absolutePath.split(separator: "/")
let relativePath = String(tokens[tokens.count-2]) + "/" + (String(tokens[tokens.count-1]))
let fileURL = directoryToSave.appendingPathComponent(imageName)
let newHeight = (image.size.height * 512) / image.size.width;
let cgSize= CGSize(width: 512, height: newHeight)
let resizedImage = image.imageResized(to: cgSize)
guard let data = resizedImage.jpegData(compressionQuality: 0.5) else { return nil }
do {
try data.write(to: fileURL, options: .atomic)
print("Immagine salvata")
return relativePath
} catch let error {
print("error saving file with error", error)
return nil
}
}
I'm taking snapshot from a PDFView in PDFKit for streaming (20 times per sec), and I use this extesnsion
extension UIView {
func asImageBackground(viewLayer: CALayer, viewBounds: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: viewBounds)
return renderer.image { rendererContext in
viewLayer.render(in: rendererContext.cgContext)
}
}
}
But the output UIImage from this extension has a high resolution which make it difficult to stream. I can reduce it by this extension
extension UIImage {
func resize(_ max_size: CGFloat) -> UIImage {
// adjust for device pixel density
let max_size_pixels = max_size / UIScreen.main.scale
// work out aspect ratio
let aspectRatio = size.width/size.height
// variables for storing calculated data
var width: CGFloat
var height: CGFloat
var newImage: UIImage
if aspectRatio > 1 {
// landscape
width = max_size_pixels
height = max_size_pixels / aspectRatio
} else {
// portrait
height = max_size_pixels
width = max_size_pixels * aspectRatio
}
// create an image renderer of the correct size
let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: UIGraphicsImageRendererFormat.default())
// render the image
newImage = renderer.image {
(context) in
self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
}
// return the image
return newImage
}
}
but it add an additional workload which make the process even worse. Is there any better way?
Thanks
You can downsample it using ImageIO which is recommended by Apple:
extension UIImage {
func downsample(to resolution: CGSize) -> UIImage? {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let data = self.jpegData(compressionQuality: 0.75) as? CFData, let imageSource = CGImageSourceCreateWithData(data, imageSourceOptions) else {
return nil
}
let maxDimensionInPixels = Swift.max(resolution.width, resolution.height) * 3
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels
] as CFDictionary
guard let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions) else {
return nil
}
return UIImage(cgImage: downsampledImage)
}
}
This question already has answers here:
The simplest way to resize an UIImage?
(34 answers)
Closed 1 year ago.
I have successfully uploaded the image to Storage, but I have a problem that when my image is large in size it will be slow in rendering to the UI. I want to resize my images to the same default size before uploading to Storage.
func uploadImageToStorage(image: UIImage) {
if let imageData = image.jpegData(compressionQuality: 1) {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}
1. Define a function to change the image size.
How to Resize image in Swift?
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
let rect = CGRect(origin: .zero, size: newSize)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
2. Call that function when uploading.
func uploadImageToStorage(image: UIImage) {
if let imageData = resizeImage(image: image, targetSize: CGSize(width: 200, height: 200))?.pngData() {
let storage = Storage.storage()
let storageRef = storage.reference()
let testRef = storageRef.child("avatar/\(user.id)/avatar.png")
testRef.putData(imageData, metadata: nil) {( _, error) in
if let error = error {
print("an error has occured - \(error.localizedDescription)")
} else {
print("image uploaded successfully")
}
}
} else {
print("Coldn't unwrap/case imgae to data")
}
}
I want to do a rectangular crop of a JPEG image. I have the following code that will create a duplicate image. It uses an NSImage. I do not know how to create a cropped image.
func crop(index: Int) {
let croppedImageUrl = ...
let imageUrl = ...
// Create a cropped image.
let data = try? Data(contentsOf: imageUrl)
let image = NSImage(data: data!)
let tiffRepresentation = (image?.tiffRepresentation)!
let bitmap = NSBitmapImageRep(data: tiffRepresentation)
let representation = bitmap?.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])
do {
try representation?.write(to: croppedImageUrl, options: [.withoutOverwriting])
} catch let error as NSError {
print(error.localizedDescription)
}
}
Something like...
func crop(nsImage: NSImage,rect: CGRect) -> NSImage {
let cgImage = (nsImage?.cgImage(forProposedRect: nil, context: nil, hints: nil)?.cropping(to: rect))!
let size = NSSize(width: rect.width, height: rect.height)
return NSImage(cgImage: cgImage, size: size)
}
Sorry, not compiled this code fragment but general method worked in my code. Probably better done as an extension to NSImage, if that is possible.
This may help you to crop image
func crop() -> UIImage? {
let imageUrl = URL(string: "imageUrl")!
let data = try! Data(contentsOf: imageUrl)
let image = UIImage(data: data)!
// Crop rectangle
let width = min(image.size.width, image.size.height)
let size = CGSize(width: width, height: width)
// If you want to crop center of image
let startPoint = CGPoint(x: (image.size.width - width) / 2, y: (image.size.height - width) / 2)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
image.draw(in: CGRect(origin: startPoint, size: size))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return croppedImage
}
I try to change the size and the resolution of an image programmatically, afterwards I save this image.
The imagesize in the imageView is changing, but when I look at my file "file3.png" it always has the original resolution of 640x1142.
I googled around but can't find a solution. I try to redraw the image. But maybe it's the wrong strategy.
thanks
#IBAction func pickOneImageBtn(sender: AnyObject) {
//load image from path
pickedImage.image = loadImageFromPath(fileInDocumentsDirectory("Angebote.png"))
let newSize = NSSize(width: 10, height: 10)
if let image = pickedImage.image {
print("found image")
//cast to CGImage
var imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height)
let imageRef = image.CGImageForProposedRect(&imageRect, context: nil, hints: nil)
if let imageRefExists = imageRef {
print("Cast to CGImage worked \(imageRefExists)")
}
//redraw to NSImage with new size
let imageWithNewSize = NSImage(CGImage: imageRef!, size: newSize)
//save on disk
let imgData: NSData! = imageWithNewSize.TIFFRepresentation!
let bitmap: NSBitmapImageRep! = NSBitmapImageRep(data: imgData!)
if let pngCoverImage = bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:]) {
pngCoverImage.writeToFile("/...correctpath.../imageSourceForResize/file3.png", atomically: false)
print("saved new image")
}
//the size is smaller
pickedImage.image = imageWithNewSize
}
}
Change
let imgData: NSData! = pickedImage.image!.TIFFRepresentation!
to
let imgData: NSData! = imageWithNewSize.TIFFRepresentation!
I tried to change the size of a NSImage for Mac application and here is the working function to resize an image written in swift.
func resize(image: NSImage, w: Int, h: Int) -> NSImage
{
let destSize = NSMakeSize(CGFloat(w), CGFloat(h))
let newImage = NSImage(size: destSize)
newImage.lockFocus()
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSZeroRect, operation: NSCompositingOperation.CompositeCopy, fraction: 1.0)
newImage.unlockFocus()
newImage.size = destSize
return NSImage(data: newImage.TIFFRepresentation!)!
}
You need to pass 3 parameters to call this function i.e NSImage, width, height and this function will return resized image.
targetimage = resize(source, w: Int(targetwidth), h: Int(targetheight))