I am trying to convert a NSImage into a base64 string encoding.
extension NSImage{
// Convert NSImage self to a string of base64 encoding
func getBase64String() -> String{
guard let tiffData = self.tiffRepresentation else {
printError("Failed to get tiffRepresentation")
exit(-1)
}
guard let bitmap: NSBitmapImageRep = NSBitmapImageRep(data: tiffData) else {
printError("Failed to get Bitmap representation from tiffRepresentation")
exit(-1)
}
guard let data = bitmap.representation(using: .png, properties: [:]) else {
printError("Failed to make image data with PNG type")
exit(-1)
}
let tiff_base64 = "data:image/png;base64," + tiffData.base64EncodedString()
let bitmap_base64 = "data:image/png;base64," + data.base64EncodedString()
return bitmap_base64
}
}
I used the result to embed images in a html file, and I found both tiff_base64 and bitmap_base64 work. However, the strings look kind of different of the same image.
Many examples of converting images into base64 on Stack Overflow are calling base64EncodedString() from a bitmap data. I am wondering does it really matter to use tiff_base64 or bitmap_base64?
TIFF and PNG are two different image formats. The use of base 64 is really irrelevant to your question.
First decide which image format you need (which depends on what you are doing with the result. Once you've decided whether you need your images represented as PNG or TIFF (or JPEG or any other supported format), then you apply the base64 encoding to that data.
Related
I have a class that conforms to NSCoding. I want to convert that class into data, then into a string that can be stored on backend.
The issue is that once it's a converted from a String back to Data it fails to unarchive properly with NSKeyedUnarchiver, so converting to a string must be corrupting the data.
Here's the code:
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: drawItems, requiringSecureCoding: false)
let stringData = String(decoding: data, as: UTF8.self)
print("HERE successfully converted to a string: ", stringData)
let stringBackToData: Data? = stringData.data(using: .utf8)
if let stringBackToData = stringBackToData, let allItems = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(stringBackToData) as? [DrawItem] {
print("HERE items: ", allItems)
} else {
print("HERE FAILED to unarchive")
}
catch {
print("Failed: ", error)
}
What is wrong with this string conversion?
The default outputFormat for NSKeyedArchiver is .binary, which is not convertible to or from UTF-8. In your example, String(decoding: data, as: UTF8.self) "repairs" invalid UTF-8 bytes by replacing them with the UTF-8 replacement character (0xFFFD).
This corrupts the archiver data, and prevents it from decoding properly.
If you really can't transmit binary data to your backend as-is, or store it that way, and must convert the data into a string, you have two options:
NSKeyedArchiver has an alternate .outputFormat of .xml, which produces UTF-8 XML data. You can produce this data by creating an archiver and configuring it:
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
archiver.outputFormat = .xml
archiver.encode(drawItems, forKey: NSKeyedArchiveRootObjectKey)
if let error = archiver.error {
// Handle the error. This is the same error that would be produced
// by `NSKeyedArchiver.archivedData(withRootObject:requiringSecureCoding:)`
} else {
let data = archiver.encodedData
// `data` is UTF-8 XML data ready to be transmitted/stored
}
Alternatively, you can continue using the convenience method you are now, but Base64 encode it:
let data = try NSKeyedArchiver.archivedData(withRootObject: drawItems, requiringSecureCoding: false)
let base64EncodedData = data.base64EncodedString()
Which you use is up to you: on its own, the base64-encoded string is shorter than the raw XML data (because the starting binary data is significantly smaller than its equivalent XML), but the XML data does compress significantly better than the base64 data does, and may very well end up smaller. You can test both approaches to see what fits your use-case better.
I'm trying to convert Base64 Svg to UIImage, but I get nil.
Everything is OK on this site https://codebeautify.org/base64-to-image-converter
I try different frameworks on native methods, but all to no avail
I use:
let dataDecoded : Data = Data(base64Encoded: base64Str, options: .ignoreUnknownCharacters)!
polygonImage.image = UIImage(data: dataDecoded)!
2) ```
if let url = URL(string: base64StrUrl) {
if let data = try? Data(contentsOf: url) {
polygonImage.image = UIImage(data: data)
}
}
let anSVGImage: SVGKImage = SVGKImage(data: data)
self.polygonImage.image = anSVGImage
Problematic base64:
data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIGhlaWdodD0nMTAwJyB3aWR0aD0nMTAwJyB2aWV3Qm94PSc5MC42NDM5NTE0MTYwMTU2MiAxNTIuMDAzOTg4NjQ3NDYwOTQgMC4wMTE2NDI0NTYwNTQ2ODc1IDAuMDExMzg2MTA4Mzk4NDM3NScgc3R5bGU9J3RyYW5zZm9ybTogc2NhbGVYKC0xKSByb3RhdGUoOTBkZWcpJz48ZyBmaWxsPSdub25lJz48cG9seWdvbiBwb2ludHM9JzkwLjY0Nzc1MDg1NDQ5MjE5LDE1Mi4wMDU2MTUyMzQzNzUgOTAuNjQ1NjE0NjI0MDIzNDQsMTUyLjAwODg5NTg3NDAyMzQ0IDkwLjY0NjYyMTcwNDEwMTU2LDE1Mi4wMTI5Njk5NzA3MDMxMiA5MC42NTI3ODYyNTQ4ODI4MSwxNTIuMDEzNzQ4MTY4OTQ1MyA5MC42NTM5MzA2NjQwNjI1LDE1Mi4wMDY2Mzc1NzMyNDIyICcgc3R5bGU9J2ZpbGw6I0ZGN0EwMDsgZmlsbC1vcGFjaXR5OjAuMjU7IHN0cm9rZTogI0ZGN0EwMDsnIHN0cm9rZS13aWR0aD0nMC4wMDAyMzI4NDkxMjEwOTM3NScgLz48L2c+PC9zdmc+
I'm pretty sure it's not a valid base64 string.
The link you have given has used some image processing techniques to generate the image, but in swift you have to provide a valid base64 string and if you download the image from the same website then you cannot open that image.
you can validate your base64 string on https://base64.guru/tools/validator
if you want to convert this base64 to image
paste your base64 string in this website and download .svg file (because you can't get a image file from base64 directly)
convert your .svg file .png or .jpg with https://svgtopng.com
convert .png or .jpg file to base64 string with https://www.base64encoder.io/image-to-base64-converter/
then use that base64 string you will definitely get image.
I have a UIImage object, say from the camera roll via PHAsset. The image is saved as a .jpg file:
asset.requestContentEditingInput(with: nil) { (input, nil) in
print(input?.fullSizeImageURL) // somefile.jpg
}
To get the file size should not data.count from this return the correct file size in bytes?
PHImageManager.default().requestImageData(for: asset, options: nil) { data, _, _, _ in
if let _data = data {
print(_data.count) // 6759240
}
}
The output for a particular image is 6759240 while fileSize() returns 2978548.0 (which is the right file size) bytes.
func fileSize(forURL url: Any) -> Double {
var fileURL: URL?
var fileSize: Double = 0.0
if (url is URL) || (url is String)
{
if (url is URL) {
fileURL = url as? URL
}
else {
fileURL = URL(fileURLWithPath: url as! String)
}
var fileSizeValue = 0.0
try? fileSizeValue = (fileURL?.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! Double?)!
if fileSizeValue > 0.0 {
fileSize = (Double(fileSizeValue))
}
}
return fileSize
}
Does it mean someUIImage?.jpegData(compressionQuality: 1)?.count does not return the correct size of JPEG image file (if saved)?
One more thing, Is there any way to determine the image file size before writing it on the disk?
All of these is to compare the file size between the original and compressed image.
This sounds like a misunderstanding of what the various terms and calls refer to.
You have no direct access to a file stored in the user's Photo library. There may in fact be no such file; you should make no assumptions about the storage format. When you ask PHImageManager for an image's data, you are given the bitmap data, ready for use. Thus you should expect this data to be big, in exact proportion to the dimensions of the image. 6759240 is more than 6MB, which sounds about right on an older iPhone; a newer iPhone, takes 4032x3024 photos which is more than 8MB.
Then in a different part of your code you call fileSize(forURL:). Now you're looking at an actual file, in the file system, in a place where you can access it. If this is an image file, it is compressed; just how much it is compressed depends on the format. 2978548 is about 3MB which is pretty good for a JPEG compressed without too much lossiness.
Finally, you ask about UIImage jpegData(compressionQuality: 1)?.count. You do not show any code that actually calls that. But this is data ready for saving as a file directly with write(to:) and a URL, and I would expect it to be the same as fileSize(forURL:) if you were to check the very same file later.
The following piece of Swift code is using the new iOS11 Vision framework to analyze an image and find QR codes within it.
let barcodeRequest = VNDetectBarcodesRequest(completionHandler {(request, error) in
for result in request.results! {
if let barcode = result as? VNBarcodeObservation {
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .isoLatin1)
print(content) //Prints garbage
}
}
}
}
let image = //some image with QR code...
let handler = VNImageRequestHandler(cgImage: image, options: [.properties : ""])
try handler.perform([barcodeRequest])
However, the problem is that the desc.errorCorrectedPayload returns the raw encoded data as it has been read from the QR code.
In order to get a printable content string from the descriptor one must decode this raw data (e.g. determine the mode from the first 4 bits).
It gets even more interesting because Apple already has code for decoding raw data in the AVFoundation. The AVMetadataMachineReadableCodeObject class already has the .stringValue field which returns the decoded string.
Is it possible to access this decoding code and use it in Vision framework too?
It seems that now you can get a decoded string from a barcode using new payloadStringValue property of VNBarcodeObservation introduced in iOS 11 beta 5.
if let payload = barcodeObservation.payloadStringValue {
print("payload is \(payload)")
}
I'm trying to convert an NSImage from an NSImageView to a Base64 string but end up losing half the quality when decoding the output.
The code to convert to Base64 seems straightforward enough which I've put into an NSString extension:
extension NSImage {
func base64String() -> String? {
guard
let bits = self.representations.first as? NSBitmapImageRep,
let data = bits.representation(using: .JPEG, properties: [:])
else {
return nil
}
return "data:image/jpeg;base64,\(data.base64EncodedString())"
}
}
Trying that with a test JPG image that's 39KB gets decoded back to 20KB. I've tried converting the same image using online tools and get a perfect encode -> decode.
Other code I've tried:
func base64String() -> String? {
let cgImgRef = self.cgImage(forProposedRect: nil, context: nil, hints: nil)
let bmpImgRef = NSBitmapImageRep(cgImage: cgImgRef!)
let data = bmpImgRef.representation(using: NSBitmapImageFileType.JPEG, properties: [:])!
return "data:image/jpeg;base64,\(data.base64EncodedString())"
}
Which results in a 17KB file.
Any help would be very much appreciated as I've racked my brain with this for hours.
You have not specified the compression value so it defaults to the default compression. To have no compression use the code below:
let data = bits.representation(using: .JPEG, properties: [NSImageCompressionFactor:1.0])
According to the documentation, NSBitmapImageRep renders the image. Your code seems to re-encode the image as jpeg again. As jpeg is a loosy algorithm, this will result in loss of quality. You can try to:
use png as representation
use a high (or 1.0) compression factor for jpeg.