Swift ios only pick string values from array - ios

In my app I have an array that looks like this:
var imgArray = [(Data, String)]()
It holds images and the images names and I append data by doing this:
if let firstImage = self.firstImage {
if let firstImageData = firstImage.compressImage() {
self.imgArray.append(firstImageData, self.randomImageName(length: 15))
}
}
Now I need to send all the image names inside that array using alamofire and I tried this:
for imgName in self.imgArray {
parameters["images[]"] = imgName.1
}
But my server responds by saying that images[] is empty, so I think that my ios code never sends an array named images[] containing the image names
Update
I changed the parameter to this:
parameters["images"] = [imgName.1]
This will upload one image, it seems like it overwrites the value each time so I need a way to grap all Strings from the imgArray array

Solution:
parameters["images"] = imgArray.map{$0.1}
This will add all of the image names from imgArray

Related

passing all members of a json array from an API call in swift

Hi have a project which I implemented ImageSlideshow and sdwebimage. Images are gotten from an API call but in the docs of ImageSlideshow SDwebImages are implemented as below
let sdWebImageSource = [SDWebImageSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080")!, SDWebImageSource(urlString: "https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080")!, SDWebImageSource(urlString: "https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080")!]
which works well but for my own project I have all the images in an array and I want to display this images in the ImageSlideshow. I do not know the number of images so it is difficult to hard code it so how can I pass the array of images. ProductServices.instance.selectedProduct?.productImg[0]) this gives me the image at the first index. the images could be up to the fifth index. how can I pass this?
You can try
var allImages = [SDWebImageSource]()
if let allStr = ProductServices.instance.selectedProduct?.productImg {
for str in allStr {
allImages.append(SDWebImageSource(urlString:str))
}
}
if allImages.isEmpty {
// display custom image // or add it's default str
}

How to get a random image from a folder in swift?

So far I have created a folder of images and I am able to assign 1 of the images to a UIImageView.
But what I want to do now is create a random generator, that will pick a random image from the folder, so if I was to create a button, I could display all of the images separately and randomly on a single UIImageView.
However, this folder is going to have more than 100 images, so I don't want to hard code each one into an array. Also, the names of the image has to be unique to the image itself.
I have been searching online but cannot find information on how to code what will suit my needs. So how would I get and display a random image(s) from a folder in swift?
You can name your images like this
image_0
image_1
image_2
Then use this code to populate your UIImageView
let numberOfImages: UInt32 = 100
let random = arc4random_uniform(numberOfImages)
let imageName = "image_\(random)"
imageView.image = UIImage(named: imageName)
Update
Since you don't want to rename your images you can create a plist file (type Array) where you put all the names of your images
And finally
func loadRandomImage() {
let path = NSBundle.mainBundle().pathForResource("Images", ofType: "plist")!
let names = NSArray(contentsOfFile: path) as! [String]
let random = Int(arc4random_uniform(UInt32(names.count)))
let imageName = names[random]
imageView.image = UIImage(named: imageName)
}
You either need to create an array of string filenames and pick a random filename from the array, or you need to use the file manager (NSFileManager) to get a list of the files in your folder and then pick from THAT array. Nether is very difficult.

Is it possible to count pictures in asset catalog with particular prefix?

Is it possible to count pictures with particular prefix in asset catalog ?
For exampe, I have images like:
Cocktail_01
Cocktail_02
...
Cocktail_nn
and other similar groups.
When my app starts, I do code like below:
var rec_cocktail = [UIImage]()
for i in 1..<32 {
if i < 10 {
let name: String = "Cocktail__0" + i.description
rec_cocktail.append(UIImage(named: name)!)
} else {
let name: String = "Cocktail__" + i.description
rec_cocktail.append(UIImage(named: name)!)
}
}
alcoholImages.append(rec_cocktail)
It's works perfect, but I have few groups to load and each one has different number of images. I have to check and change range each time when I add or remove picture from asset catalog.
It might be easier to use folders instead of Images.xcassets. Create a hierarchy you like on your computer and drag this into your Xcode project. Make sure you select:
Create folder references for any added folders
Then because you now have folder references when building your app you can iterate over items inside those folders using a loop.
For example I dragged in a folder called "Cocktail" and created the reference. Now i can iterate over the items inside this folder using:
let resourcePath = NSURL(string: NSBundle.mainBundle().resourcePath!)?.URLByAppendingPathComponent("Cocktail")
let resourcesContent = try! NSFileManager().contentsOfDirectoryAtURL(resourcePath!, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles)
for url in resourcesContent {
print(url)
print(url.lastPathComponent)
print(url.pathExtension!) // Optional
}
The url.lastPathComponent is the filename of the image (e.g. Cocktail_01.jpeg) and the url itself is the complete path of the image.
If you maintain a structure of folders it is very easy to iterate over them, if you want all the images in the same folder than you can create an array with just the image names you need and iterate over that using:
// The Array of Image names
var cocktailImagesArray : [String] = []
// Add images to the array in the 'for url in resourceContent' loop
if (url.lastPathComponent?.containsString("Cocktail")) {
self.cocktailImagesArray.append(url.lastPathComponent)
}
This way you have all the images which contain Cocktail and have added them to your array. Now you can simple iterate over your freshly created array using:
for imageName in self.cocktailImagesArray {
// Do something
}

Saving and retrieving images with correct order

I'm trying save some images to an array, and their name to another array as string with below code;
self.countryNamesArray.append(object["CountryName"] as! String)
let image = UIImage(data:imageData!)
self.countryImages.append(image!)
But it doesn't retrieve images with same order I saved while retrieve CountryNamesArray with correct order.
rowString = countryNamesArray[row]
myImageView.image = self.countryImages[row]
Should I use custom class to save images with string or are there any way to fix?

Reading values .plist swift

I have a .plist in Swift. It is set up as follows
I have trouble finding information on how to read information from a .Plist.
I want to be able to randomly select one of the 845 Items in the EmojiList. Once I have that Item, then I want to have access to that Item's emoji string value and its description string value.
Programatically how would I go about accessing a random item inside the item list? And then having access to that specific item's properties?
First load the plist in to an array of dictionaries. Each dictionary represents one emoji. Then generate a random index of the array, and pull out the dictionary at that index. Now use the dictionary to access the properties of the emoji.
var emojiArray: NSArray?
if let path = NSBundle.mainBundle().pathForResource("name-of-file", ofType: "plist"){
emojiArray = NSArray(contentsOfFile: path)
}
if let array = emojiArray {
let randomIndex = arc4random_uniform(array.count) // random number from 0 to array.count - 1
let emojiDictionary = array[randomIndex]
println("emoji value: \(emojiDictionary["emoji"]), emoji description: \(emojiDictionary["description"])"
}

Resources