Reputation: 421
I am taking a photo from the camera or the Photo gallery and saving it in my application. I don't need such a large image so I am using an extension to reduce it. Then I am saving it to the application space. But the size reduction is not saved.
I have tried various permutations on size change, delete and save.
let path = "/var/mobile/Containers/Data/Application/0595DE63-AE43-4DCF-9BD0-FB552844ECF5/Documents/tour15/H25.jpg"
print("A",hotspotImage.image!.size)
hotspotImage.image = hotspotImage.image!.resizeImage(2048, opaque: true)
print("B",hotspotImage.image!.size)
let fileManager = FileManager.default
try! fileManager.removeItem(atPath: path)
try! hotspotImage.image!.jpegData(compressionQuality: 0.8)!.write(to: URL(fileURLWithPath: path), options: [.atomic])
print("C",hotspotImage.image!.size)
let imageTest = UIImage(contentsOfFile: path)
print("D",imageTest!.size)
The results are ...
A (4032.0, 3024.0)
B (2048.0, 1536.0)
C (2048.0, 1536.0)
D (4096.0, 3072.0)
When I retrieve the "updated" image it has the original size. You cannot see this but the image has definitely been replaced.
Obviously I am missing something fundamental - please put me out of my misery.
Apologies to those kind people who responded previously - but I have now discovered some extra and important information. The example size was fooling us all. When I changed the reduced size to 200 (and not exactly half the original size) I get the following results:
A (4032 3024)
B (200 150)
C (200 150)
D (400 300)
So what ever is happening is doubling the size - not - as I previously thought - not updating with the new size.
Does this make it clearer or more of a puzzle?
And talking of puzzles I got a -1 but no one said why.
Upvotes: 1
Views: 632
Reputation: 1977
I recently ran into a similar issue. I was resizing the image with
func resizeImage(toSize: CGSize) -> UIImage? {
guard let cgImg = cgImage else { return nil }
let scale = cgImg.size().aspectFitScale(toSize)
let newImage = UIImage.init(cgImage: cgImg, scale: 1/scale, orientation: imageOrientation)
return newImage
}
The pngData and jpegData that was saved was the original image without any resizing. In order to see any resizing I had to resize by creating a new CGImage.
func resizeImage_other(toSize: CGSize) -> UIImage? {
let scaleByWidth = toSize.width / size.width
let scaleByHeight = toSize.height / size.height
let finalScale = CGFloat.minimum(scaleByWidth, scaleByHeight)
let newHeight = size.height * finalScale
let newWidth = size.width * finalScale
UIGraphicsBeginImageContext(CGSize.init(width:newWidth, height:newHeight))
draw(in: CGRect.init(x:0, y:0, width:newWidth, height:newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
Upvotes: 2
Reputation: 36
If it is automatically doubling the desired size, it sounds as if screen scaling is being applied. I would examine the extension being used. If scaling is set to 0.0, then the current screen scaling will be applied.
Screen scaling is great for images that appear onscreen. (That is why it is called screen scaling.) But creating an image whose destination is not the screen, scaling should be set to 1.0 to keep the desired size and NOT double or triple it.
Upvotes: 0
Reputation: 2107
UIImage(named:)
caches images. You're not reading from disk the second time you call it (I guess you called it somewhere before with the same path). To bypass the cache you need to use init(contentsOfFile:)
The following should work as intended:
let path = getAppFolder() + photoUrl
print("A",hotspotImage.image!.size)
hotspotImage.image = hotspotImage.image!.resizeImage(2048, opaque: true)
print("B",hotspotImage.image!.size)
let fileManager = FileManager.default
try! fileManager.removeItem(atPath: path)
try! hotspotImage.image!.jpegData(compressionQuality: 0.8)!.write(to: URL(fileURLWithPath: path), options: [.atomic])
print("C",hotspotImage.image!.size)
let imageTest = UIImage(contentsOfFile: path)
print("D",imageTest!.size)
Upvotes: 0