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!
I have an application where user can upload multiple images and all the images will be stored in a server and will be displayed on a web view in my iOS application.
Now everything used to work just about fine till iOS 10 but suddenly we started seeing some pictures/ images not being displayed , after a little debugging we found out that this is the problem caused because of the new image format of apple (HEIC),
I tried changing back to the Native UIImagePicker (picks only one image) and the images are being displayed as Apple I guess is converting the Image from HEIC to JPG when a user picks them, but this is not the case when I use 3rd party libraries as I need to implement multiple image picker.
Though we are hard at work to make the conversion process on the server side to avoid users who have not updated the app to face troubles, I also want to see if there is any way in which I can convert the image format locally in my application.
There's a workaround to convert HEIC photos to JPEG before uploading them to the server :
NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.7);
If you use PHAsset, the, in order to have the image object, you'll need to call this method from PHImageManager:
- (PHImageRequestID)requestImageForAsset:(PHAsset *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *__nullable result, NSDictionary *__nullable info))resultHandler;
On server side you also have the ability to use this API or this website directly
I've done it this way,
let newImageSize = Utility.getJpegData(imageData: imageData!, referenceUrl: referenceUrl!)
/**
- Convert heic image to jpeg format
*/
public static func getJpegData(imageData: Data, referenceUrl: NSURL) -> Data {
var newImageSize: Data?
if (try? Data(contentsOf: referenceUrl as URL)) != nil
{
let image: UIImage = UIImage(data: imageData)!
newImageSize = image.jpegData(compressionQuality: 1.0)
}
return newImageSize!
}
In Swift 3, given an input path of an existing HEIF pic and an output path where to save the future JPG file:
func fromHeicToJpg(heicPath: String, jpgPath: String) -> UIImage? {
let heicImage = UIImage(named:heicPath)
let jpgImageData = UIImageJPEGRepresentation(heicImage!, 1.0)
FileManager.default.createFile(atPath: jpgPath, contents: jpgImageData, attributes: nil)
let jpgImage = UIImage(named: jpgPath)
return jpgImage
}
It returns the UIImage of the jpgPath or null if something went wrong.
I have found the existing answers to be helpful but I have decided to post my take on the solution to this problem as well. Hopefully it's a bit clearer and "complete".
This solution saves the image to a file.
private let fileManager: FileManager
func save(asset: PHAsset, to destination: URL) {
let options = PHContentEditingInputRequestOptions()
options.isNetworkAccessAllowed = true
asset.requestContentEditingInput(with: options) { input, info in
guard let input = input, let url = input.fullSizeImageURL else {
return // you might want to handle this case
}
do {
try self.save(input, at: url, to: destination)
// success!
} catch {
// failure, handle the error!
}
}
}
private func copy(
_ input: PHContentEditingInput, at url: URL, to destination: URL
) throws {
let uniformType = input.uniformTypeIdentifier ?? ""
switch uniformType {
case UTType.jpeg.identifier:
// Copy JPEG files directly
try fileManager.copyItem(at: url, to: destination)
default:
// Convert HEIC/PNG and other formats to JPEG and save to file
let image = UIImage(data: try Data(contentsOf: url))
guard let data = image?.jpegData(compressionQuality: 1) else {
return // you might want to handle this case
}
try data.write(to: destination)
}
}
I load the image using my Android application Parse in my database, and I want to show it on my iphone.
when I run the Xcode tells me: 'Could not release the value of type' NSConcreteData 'to' PFFile ''
there any way to get this image and show on my iPhone (UIImageView) ??
Any idea to help me ?
I'll be very grateful !!
enter image description here
The problem is, as the Compiler tells you, that you're trying to cast NSData to PFFile.
Just use:
let userImageProfilePicture = event["profile_picture"]
userImageProfilePicture.getDataInBackgroundWithBlock({ (data, error) -> Void in
// handle here with your userAuth == true block
if let data = data where error == nil{
var image = UIImage(data: data)
}
})
I get the error in Alamofire.downloand as below:
Alamofire.download(URLString).responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
}else{
print(response.result.error)
}
}
Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///private/var/mobile/Containers/Data/Application/40167F58-FF4A-4D19-B01A-F8ED90F794DD/tmp/CFNetworkDownload_1dnNQR.tmp)))
Can anyone help solved it or facing same issues?
Thanks
Change the Alamofire.download to Alamofire.request, it will download. Exact answer for your question, check here
I'm trying to present images from url in a collection view. So far I was using AFNetworking for this, but now I'm moving to Alamofire.
I can not find a proper way to present the image like I was doing with the method 'setImageWithURLRequest: placeholderImage: success:'
Do I need to use AFNetworking in order to do this right? Is there a Alamofire way to do this that I'm skipping?
Thanks in advance
Edit
I'm currently using AlamofireImage
Here is the code I'm using:
Alamofire.request(.GET, urlString)
.responseImage { response in
if let image = response.result.value {
self.imageView.image = image
}
}
My problem is that this code is inside 'cellForItemAtIndexPath' method, and every time I scroll the images are recharged again and again. This wasn't happening with AFNetworking
You want to use the UIImageView extension in Alamofire to set the image from a URL. There is some very detailed documentation in the README.
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
imageView.af_setImageWithURL(URL)
Best of luck!