how to pass [String]' to 'String' value in swift - ios

I am trying to create a slideshow
I follow this zvonicek/ImageSlideshow library.
But I don't know how to pass array of images to it
I have the following code:
let imageSource = [SDWebImageSource(urlString:self.myArray[i])!, ImageSource(image: UIImage(named: "Beach")!)]
slideshow.setImageInputs(imageSource as! [InputSource])
self.myArray[i] contains that kind of value
and I need to pass that array to imageSource.

Essentially, what you are trying to do is to transform each element of a [String] to a SDWebImageSource, so that it forms a [SDWebImageSource]. Right? This is precisely the job of the map method.
let imageSource = self.myArray.map { SDWebImageSource(urlString: $0) }
Or more simply:
let imageSource = self.myArray.map(SDWebImageSource.init))

As mentioned in the loading images section in the description of the library, if you have url for the images. You can use the AlamofireSource or KingfisherSource to create the image Source array.
ImageSource(image: UIImage(named: "myImage"))!,
ImageSource(image: UIImage(named: "myImage2"))!,
AlamofireSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080"),
KingfisherSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080"),
ParseSource(file: PFFile(name:"image.jpg", data:data))
])

Related

Mapbox : MGLSymbolStyleLayer add my custom "iconImage"

How can i add custom image for MGLSymbolStyleLayer. Below is my code,
let symbolGraphicsLayer = MGLSymbolStyleLayer(identifier: identifier, source: source)
symbolGraphicsLayer.sourceLayerIdentifier = identifier
symbolGraphicsLayer.iconImageName = MGLStyleConstantValue<NSString>(rawValue: "assets/myImage")
symbolGraphicsLayer.iconScale = MGLStyleValue(rawValue: 1)
symbolGraphicsLayer.isVisible = true
self.mapView.style?.addLayer(symbolGraphicsLayer)
Thanks.
Is your problem that the image doesn't appear?
You first need to add the image to the style layer, and then you can use it.
So before that code, you can do:
if let image = UIImage(named: "myImage") {
mapView.style?.setImage(image, forName: "myImage")
}
and you can use it afterwards like you said. Just use the name you passed to the setImage method.
I hope this helps other people since the documentation is quite poor for this

how to add image literal to an array and show them?

I was trying to add image literals to array and display them as per the index.
Here is my code :
var images = [#imageLiteral(resourceName: "male-circle-128"),#imageLiteral(resourceName: "add_to_favourite-128"),#imageLiteral(resourceName: "28468-200"),#imageLiteral(resourceName: "progress_circular"),#imageLiteral(resourceName: "logout-1-128")]
and showing like this
cell!.imageView?.image = UIImage.init(cgImage: images[indexPath.row] as! CGImage)
got EXC_BAD_INSTRUCTION! what is the proper way to do this
let images:[UIImage] = [array of image literal goes here]
You can simply create array of image literals like:
var images = [#imageLiteral(resourceName: "image1"),#imageLiteral(resourceName: "image2"]
Why do you cast to CGImage , your literals are UIImage and you can use them without casting and even if you want CGImage use the initializer not casting.
let images = [literal images are here]
imageview.image = images[indexPath.row]

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.

Want to use string to UIImage(named: String).center

I created the String array called Pillar and there are 26 Strings.
I want to use that Strings in Array as a UIImage's name and set that UIImage a CGPoint.
But there is an error that UIImage does not have the member center.
The error is:
Value of type 'UIImage?' has no member 'center'
The code is
var Pillar:[String]=["Pillar1","Pillar2","Pillar3",...,"Pillar26"]
self.Pillar1.center = CGPointMake(175.0, 436.0)
self.Pillar2.center = CGPointMake(214.0, 407.0)
for(let i=3; i<=26; i++) {
//here the error I occur
UIImage(named: Pillar[i]).center = CGPointMake(UIImage(named: Pillar[i-1]).center.x-5,UIImage(named: Pillar[i-1].center.y+4)
}
I just want to make iteration to emphasise the code by using the String array.
If not I have to write 26 lines of code. If anyone can help me please help.
Try this:
let images = ["Pillar1","Pillar2","Pillar3",...,"Pillar26"].map { UIImage(named: $0) }
And note that UIImageView has center property, NOT UIImage

Cannot subscript a value of type anyobject

I've currently upgraded to the new Xcode 7, and the following code never had any errors with swift 1.2, but now its telling me that :
Cannot subscript a value of type any object
var imageArray : NSArray = []
let url = NSURL(string: (self.imageArray[indexPath.row][0] as? String)!)
I know its about [0] but how do i rewrite it to be accepted ?
OK, so first you are using an NSArray. You can drop that and make everything much easier.
In Swift always use strong typing where possible. Avoid Any and AnyObject when they are not needed. (They are VERY RARELY needed).
The error is happening because you're not telling the code what is actually in the imageArray.
Also, imageArray tells me the array is full of images. Name your variables more descriptively. imageUrlStringArray or arrayOfImageUrlArrays. Something more descriptive anyway.
Declare imageArray like...
var imageArray = [[String]]()
This tells the compiler that imageArray is a 2D array with Strings at the second level.
Now you can create your URL easily...
guard
let urlString = imageArray[indexPath.row].first,
let url = NSURL(string: urlString)
else { // Handle the inability to create a URL }
// Do something with the url

Resources