How to upload image with parameter multipart in swift - ios

I attached the screen shot of request and response. How to pass this type of parameter, i am not able add this type parameter in request. Please make a structure/sample so i can use in my code. Any clarification/suggestion please let me know. thanks in Advance.

let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
let imageName = "imageClicked.png" // name of the image to be saved.
//let localPath = documentDirectory.appending(imageName)
// imageName which you have stored in the db at retrieval time
let imagePath = String("\(documentDirectory)/\(imageName)")
let fileName = (UIImage(named: imagePath)?.pngData())!
// Here i am selecting the image from source and getting the image path and converting into data. then getting the error.
// if i am taking image from app and converting into data getting the response.
// Passing the image from App
let fileName = (UIImage(named: "swift_file.png")?.pngData())!

Related

Saving images on Xcode simulator

I'm trying to save images using their file paths but every time that I kill the Xcode simulator and restart it, the location of the images changes and my imageView is blank. I know for a fact that the location changes because I print out the file path when I try to pick an image from the photo library and it's different each time.
I access the URL of the UIImage
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
let url = info[UIImagePickerController.InfoKey.imageURL] as! URL
After which I save the URL as a string to my realm database. I've tried converting it to string via all of these
let imageFilePath = url.relativeString
let imageFilePath = url.absoluteString
let imageFilePath = url.relativePath
but none of them worked. I'm a newbie to Swift so let me know if I've missed any key details needed.

how to load image from local image path Ios Swift 4

I have one JSON which contain path of images from one local folder of Project as Followed
The issue is i want to get image from that path, How do i achieve it?
I had try to convert String to URL and set URL as Image using Kingfisher Library
let url = URL(string: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")!
cell.imgTool.kf.setImage(with: url)
But it don't work I had Tried this one also
let url = URL(string: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")!
let imageData:NSData = NSData(contentsOf: url)!
let image = UIImage(data: imageData as Data)
cell.imgTool.image = image
NOTE: I can't upload this JSON file on Server, I need to use it Locally
I have solved my issue using fileURLWithPath
let url = URL.init(fileURLWithPath: "/VWiOSProjects/CollageMakerDemo/Development/CollageMaker/CollageMaker/Goodies.xcassets/Goodies-1.imageset/Goodies-1.png")
let imageData:NSData = NSData(contentsOf: url)!
let image = UIImage(data: imageData as Data)
cell.imgTool.image = image
If the images are in Assets(*.xcassets) folder, the you can access it by init(named:) method.
cell.imgTool.image = UIImage(named: "img1")
Actually you no need to store the entire path. You could store image names in array or something.
In my point of view, the best way is to add All Images to *.xcassets folder.(Because you have preloaded 5-10 images)
In case you needs to display it in collection view or TableView,
let imageName = "Goodies-\(indexPath.row)"
cell.imgTool.image = UIImage(named: imageName)
If images are in assets folder itself then go for #Lal Krishna's method. And if they are on server then you should add http:// or https:// and the URL of server followed by JSON's URL.
If still you are not getting it then let me know.

AlamofireImage download into CoreData

i want to cache image and few data from json to make persistent I know Alamofire Image has it own chade method, but i guess is only while app is alive when app close and open again chache is cleared so i have to download again image. so
if my image are download like so
image.af_setImage(withURL: url)
it posible to save
imageEntity.save(image.image)
not sure to be exactly code but the idea is to save the image downloaded an set to image in CoreData.
and any idea behind the logic of this approach to cjeck if image exist in coreData and show from the if not download and save to coreData
CoreData is not designed to store ImageData or large data (files) but only String, Dates, integer... Just like a normal database.
If you want to save a data you could use AlamofireImageCache and save the image identifier in the CoreData Entity as a string. It's the most easy way to do it.
let imageCache = AutoPurgingImageCache(
memoryCapacity: 100_000_000,
preferredMemoryUsageAfterPurge: 60_000_000
)
imageCache.add(yourImage, withIdentifier: "YourImageIdentifier")
imageEntity.identifier = "YourImageIdentifier"
Then if you want to get your image you can do for example:
let cachedAvatar = imageCache.image(withIdentifier: imageEntity.identifier)
please check AlamofireImage documentation for more information about cache: https://github.com/Alamofire/AlamofireImage
Update
If you want to make your own ImageCache on the hard-drive here you have an example of storing an Image:
func saveImage(image:UIImage){
if let data = UIImagePNGRepresentation(image) {
let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]!
let filename = documentPath.appendingPathComponent("YourImageName")
try? data.write(to: filename)
}
}
func getImage(ForName name:String) -> UIImage?{
let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]!
let filename = documentPath.appendingPathComponent("YourImageName")
return UIImage(contentsOf:URL(fileURLWithPath: filename))
}
Hope it helps !
Pierre

Load image from folder inside project

I'm making an app with the folder like image below:
As you can see in this picture, Stickers folder have 2 sub folder "1" and "2". In side them is bunch of icon and I wanna load it in a collection view with each sub folder is a package of different sticker. For more details, please see this picture:
So, how can I get it in my project folder? I've read about access document directory but seem like it not solve my problem.
Please help me. Thanks in advance.
Look at Filemanagers API. It offers all you need.
Example to get content at path:
let pathToDir1 = Bundle.main.resourcePath! + "/Stickers/1";
let fileManager = FileManager.default
let contentOfDir1 = try! fileManager.contentsOfDirectory(atPath: pathToDir1)
Example to iterate over:
let docsPath = Bundle.main.resourcePath!
let enumerator = fileManager.enumerator(atPath:docsPath)
let images = [UIImage]()
while let path = enumerator?.nextObject() as! String {
let contentOfCurDir = try! fileManager.contentsOfDirectory(atPath: path)
// do whatever you need.
}
For details see Apples documentation and sample code.
If you're looking to access an image within your project folder you simply need to use the image name. You don't need to define the whole path.
Objective-C
UIImage *img = [UIImage imageWithName:#"IMAGE_NAME"];
swift
var img = UIImage(named:"IMAGE_NAME")

issue while loading image from image Path iOS Swift

well am trying this :
if let storedCoverImagePath = UC.coverImagePath{
let storedCoverImage = UIImage(contentsOfFile: storedCoverImagePath as String)
self.coverImage.image = storedCoverImage
self.backgroundImageView.image = storedCoverImage
}
and the value of storedCoverImagePath is :
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
its straight forward that am trying to get an image from this storedCoverImagePath path. i double checked the path, image is there still am not able to set the image using this path, is there is something that am missing if yes than tell me please
You have to specify path relative to your apps bundle or directory. If you hardcode the path like you did:
/Users/remy/Library/Developer/CoreSimulator/Devices/D29D4F6F-E146-419D-B4B8-B1914F56569F/data/Containers/Data/Application/637675BB-EFA1-A8RT-8B73-A3628/DocumentsCoverImage.jpg
it gets ignored because of the sandboxing. iOS does not allow the app to reference directories external to the sandbox.
Possible solutions
One way to get it working is to add the image to the project. Then you can get it using:
let image : UIImage = UIImage(named:"ImageName")
Another way, if the image is saved in documents directory of your app, is to use:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent(yourImageName)
let image = UIImage(contentsOfFile: fileURL.path!)

Resources