I have some SVG files that are not loading up in iOS. (Swift 5, Xcode 13.2.1) Here is the one of those SVG: https://svgshare.com/i/dWy.svg
There are below alternatives that I've tried:
I'm using SDWebImageSVGKitPlugin library to load up the SVG from the server. The error that I get from the library is:
"Downloaded image decode failed"
UserInfo={
NSLocalizedDescription=Downloaded image decode failed
}
I tried below code to directly download image from server in data format and convert it into UIImage:
if let url = URL(string: "https://svgshare.com/i/dWy.svg") {
do {
let imageData = try Data(contentsOf: url)
if let image = UIImage(data: imageData) {
print(image)
}
} catch {
print("Error downloading image: \(error)")
}
}
I tried SwiftSVG library but the SVG image is not loading up and also there is no error in the console too.
Observation:
One strange thing is happening with this is that if I download the SVG from server, add it to the project in Assets.xcassets folder than that particular SVG is being loaded properly but when received from server it is not being loaded.
Any help will be appreciated.
Also if any additional information is required than please let me know.
Thanks in advance!
Related
I am getting an Image as Data from server.To show it I am using UIImage?(data:) to show it.For a specific image application is getting crash because of memory pressure. What could be the reason, how to fix it.
if let imgData = Utils.fetchDataFromDocumentDirectory(imageName:"test.jpg"){
attachmentImgView.image = UIImage(data:imgData)
}
How big is the Image? and are you downloading only 1 image or an array?
This might be caused from the size of the image you are trying to download.
My advice if you are trying to show an image, only giving it the url like so:
let url = URL(string: "http://i.imgur.com/w5rkSIj.jpg")
let data = try? Data(contentsOf: url)
if let imageData = data {
let image = UIImage(data: data)
}
even better you can try to use AsyncImageView 3rd party library where you give it the url of the image and it shows it asynchronously without delays in the app.
Hope this helps!
func loadCell(personObj:Person){
self.lblTitle.text = personObj.title
self.lblDesc.text = personObj.desc
print("ImgUrlInCell:", personObj.imageUrl)
do {
self.imgPicture.image = try UIImage(data:NSData(contentsOf:NSURL(string: personObj.imageUrl) as! URL) as Data)
} catch {
print("Error:",error)
}
}
Firstly, I've parsed data from API and put it in object and then insert it into database. At the time of displaying data I fetched them from database and list them in Tableview.
If there is an internet connection everything is working fine
If there is no internet connection image url doesn't work, It prints error 6 times. (I have 10 images in database or API)
Also, There are 10 image url displaying in console in ImgUrlInCell:
Please someone help me with this issue. What have I wronged.
You are using TableView and in TableView as per screen size and Cell size, it will show max cell in your screens so it may be possible that in your case its loading 6 cells data and all 6 cells have load image method so its throwing error 6 times.
To load Images lazily there are many famous libraries which will manage single time download and cache that image.
SDWebImage is one of the best libraries fits for your problem.
Link : SDWebImage
And follow below code to get Image from the server.
Swift:
import SDWebImage
imageView.sd_setImage(with: URL(string: "http://www.example.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
Put an above line of code in your loadCell method with your imageURL.
Please check Internet connection by using Apple Reachability classes and load your image if the Internet connection is there.
Edit:
For Locally storing image you can use below function of SDWebImage Downloader and cache your image before showing it to the user.
SDWebImageManager.shared().imageDownloader?.downloadImage(with: NSURL(string: strURL) as URL!, options: SDWebImageDownloaderOptions.continueInBackground, progress: {
(receivedSize, ExpectedSize, imageURL) in
print("receive:",receivedSize,"Expected:",ExpectedSize,"Image URL:",imageURL ?? "No URL")
}, completed: {
(image, data, error, finished) in
print("Image Data:",data ?? "No Data")
// Do your stuff to store downloaded image
})
Link : Reachability Sample
Hope this will helps.
I have an iMessage app that displays some remote content using SDWebImage. The images are downloaded and cached on disk. After choosing an image, I want to attach it to the message as a plain UIImage (not a MSMessage).
Here's the code I'm using
// image is already downloaded
let cache = SDImageCache.shared()
let key = remoteImageUrl
let fileUrlString = cache.defaultCachePath(forKey: key)!
let fileUrl = URL(string: fileUrlString)!
// image holds the correct UIImage
let image = UIImage(contentsOfFile: fileUrlString)
activeConversation?.insertAttachment(fileUrl, withAlternateFilename: "a funny gif", completionHandler: { (error) in
// error is nil here
print("error: \(error)")
})
Here's what the message looks like
It seems like the Messages framework can't find the image at that path.
Note: after tapping send, I the iMessage app crashes "MobileSMS quit unexpectedly."
I found out that I needed to use
let fileUrl = URL(fileURLWithPath: fileUrlString)
Hope this helps someone else
I am creating a iOS swift application that uses an SDK that requires a link to image that can be seen in browser for example http://i.imgur.com/HALb2yN.jpg is acceptable as it does not automatically download.
However for firebase the download URL I get using the code:
let imageRef = String(format: "images/%#", file.identifier)
let testRef = storageRef.child(imageRef)
let uploadTask = testRef.putData(imageData, metadata: nil) { metadata, error in
if (error != nil) {
// Uh-oh, an error occurred!
print(error?.localizedDescription)
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
let downloadURL = metadata!.downloadURL()!.absoluteString
print(downloadURL)
}
}
from downloadURL automatically downloads... I would like to be able to get a link that can be viewed in the from a browser. Is that possible with firebase?
I suspect the issue is that you haven't set any metadata with the image. Therefore Firebase (and your browser) doesn't know that your mystery binary object is an image that can be displayed in-browser instead of some other file that needs to be downloaded.
Try creating a metadata object with the appropriate content-type and use that in your putData call, and I betcha that'll work.
I'm trying to download an image called "aaa.jpeg" in my s3 bucket using AWSMobileHubHelper. I found this function on their documentation site.
func downloadContent(content: AWSContent, pinOnCompletion: Bool) {
content.downloadWithDownloadType( .Always, pinOnCompletion: pinOnCompletion, progressBlock: {(content: AWSContent?, progress: NSProgress?) -> Void in
// Handle progress feedback
}, completionHandler: {(content: AWSContent?, data: NSData?, error: NSError?) -> Void in
if let error = error {
print("Failed to download a content from a server.)")
// Handle error here
return
}
// Handle successful download here
if let image = UIImage(data: data!){
self.imageView = image
}
})
}
Once the download is successful (it Did, There is no error message), I'm trying to assign the image to an imageView.
I can tell that the data has been downloaded successfully. I can print the data and see a familiar binary structure of an image. But for some reasons I can't assign the UIImage to the imageView. Because I can't convert the data to a UIImage.
I just want to know if this is the proper way to download image from s3 or am I missing something. Does "data" in the completion block carry the downloaded image? I can't seem to find any documentations on this.
Is this the correct function to use to download from S3?
Yes, the data contains the actual image data. You can put the downloaded data in an UIImageViewController and it should open up fine. Also, this is demonstrated in a Sample App which can be downloaded from the Mobile Hub Console.