1

I'm working on app to do paged photos inside scrollViews like in the photos app to swipe right to get the old photos and swipe left to get the new photos until the end of photos.

Alright,so i am getting the photos as a multi-diminutional from the web :

imageArray[indexPath.row][0] as? String

Thats in the ViewController1 to show all the images in CollectionView .

When the user press on a photo i do segue and show the image larger in ViewController2 so if swipe left it show the new photos and right to show the old photos which is stored in the array.

but i need to covert my two-dimensional array to one and use it dynamically to be something like this :

pageImages = [UIImage(named:"photo1.png")!,
    UIImage(named:"photo2.png")!,
    UIImage(named:"photo3.png")!,
    UIImage(named:"photo4.png")!,
    UIImage(named:"photo5.png")!]

how is it possible to do ?

i could say like :

pageImages = [UIimage(named:thewholearray)] ?

i tried first to convert to one-diminutional array but i failed :

var imageArray : NSArray = []

var mybigarray : NSArray = []
    for (var i=0; i<=self.imageArray.count; i++) {

        self.mybigarray = self.imageArray[i][0] as! NSArray

    }

which generate this casting error :

Could not cast value of type '__NSCFString' (0x196806958) to 'NSArray' (0x196807308).

2 Answers 2

1

You can use the map-function to extract the image names from your multi-dimensional array.

var imageNames:[String] = imageArray.map({
    $0[0] as! String
})

The map function iterates through all array entries like a for-in-loop. The statement in the closure determines the new entry of your imageNames array.

EDIT:

If you don't use Swift-Arrays:

var imageNames:NSMutableArray = []
for image in imageArray{
    imageNames.addObject(image[0] as! String)
}
Sign up to request clarification or add additional context in comments.

14 Comments

NSArray doesn't have a member called map ?
Great, one last thing if i need to use imageNames as UIImage array as my question how is it possible to do ?
imageNames.addObject(UIImage(named: "\(image[0] as! String)")!)
@AaoIi, what I wrote will be images. I don't understand what else you want.
please clarify your question
|
0

I looked at the tutorial and I think this will help with the answer from Daniel. Keep Daniel's Answer and use the URLs from the array with the extension.
Add this to create you images from a URL

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = NSURL(string: urlString) {
            let request = NSURLRequest(URL: url)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                if data != nil {
                    self.image = UIImage(data: data)
                }
                else {
                    self.image = UIImage()
                }
            }
        }
    }
}

Use this in the PagedImageScrollViewController:

newPageView.imageFromUrl(pageImages[page])

pageImages is your array of Strings.

2 Comments

Sure i will keep it , I am using SDWebImage library to show the images, so how is it possible to do it without the extension ? github.com/rs/SDWebImage , i tried to do like let newPageView = UIImageView(image: pageImages[page] as! UIImage) newPageView.sd_setImageWithURL(url) but it says Could not cast value of type '__NSCFString' (0x196806958) to 'UIImage' (0x19746a698).
Sorry, I'm not familiar with the SDWebImage library. The error looks like it occurs because your pageImages is an array of strings and you're trying to set a String to an Image. I can look into the SDWebImage library some time later if you can't get a solution. Maybe you can manipulate the extension to fit your needs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.