Base64 Svg to UIImage return nil - swift

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.

Related

Swift: Get the correct file size for JPEG 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.

Difference between tiffRepresentation and bitmapRepresentation in terms of base64 string encoding?

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.

data from base64 url

I have a URL in the form of
foo://?data:application/x-foo;base64,OjAyMDAwMDA0MDAwMEZBDQo6MTAwMDA...
and now need to extract the base64 data into a Data object.
Unfortunately it seems the Data object does not support this yet as
let data = try Data(contentsOf: url)
returns NSURLConnection finished with error - code -1002 when trying.
While I could decode the URL manually I am wondering if I am missing a simple standard way of doing this. How would you do this?
Actually you can decode Base64 data from an URL (see for
example Base64 Decoding in iOS 7+ where this is demonstrated in Objective-C). The format is a bit different from what
you have:
let url = URL(string: "data:application/octet-stream;base64,SGVsbG8gd29ybGQh")!
let data = try! Data(contentsOf: url)
print(String(data: data, encoding: .utf8)!) // Hello world!
(Error checking omitted for brevity.)
You have to separate the base64 encoded part of the URL from the other parts, decode it, then join the original non-encoded part with the decoded part and get the data from there.
extension URL {
init?(partialBase64Url: String){
guard let base64part = base64Url.components(separatedBy: "base64,").last, let base64Data = Data(base64Encoded: base64part), let decodedString = String(data: base64Data, encoding: .utf8) else {
return nil
}
let decodedUrl = base64Url.components(separatedBy: "base64,").dropLast().joined() + decodedString
self.init(string: decodedUrl)
}
}
let decodedUrl = URL(partialBase64Url: "foo://?data:application/x-foo;base64,dGVzdFVybFN0cmluZw==")
Value of decodedUrl: "foo://?data:application/x-foo;testUrlString", as expected, since dGVzdFVybFN0cmluZw== is the base64 encoded value of testUrlString.

Swift base64 not ok

I'm using the Vapor framework to download a PDF file :
let logpw = "myLogin:passWord"
let url = "https://someRESTsite/invoices/1.pdf"
let utf8str = logpw.data(using: String.Encoding.utf8)
let base64Encoded = utf8str?.base64EncodedString()
let response = try drop.client.get(url, headers:["Authorization":"Basic " + base64Encoded!])
this works fine. Note that I'm base64 encoding the login/pw for basic auth and this works for logging in and getting the file. Now I have to embed that PDF into an XML file in base64 using similar code:
if let bodyBytes = response.body.bytes {
let bodyByteString = try String(bytes: bodyBytes)
let utf8String = bodyByteString(using: String.Encoding.utf8)
let base64String = utf8String?.base64EncodedString()
}
the result is a string that actually really looks like a base64 string, but it's different than converting the same PDF using an online tool (I tried 2 which gave the same result).
Where is my mistake?

NSImage to Base64 string loses quality

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.