0

I am able to fetch json from an api. I have populated all the other views except the imageView. I have an array of images which are string urls. I want to download these images. I'm currently using SDWebImage. I need to convert this array into a string one so i can fetch the images. I'm currently getting an error saying this since url is supposed to be string

Cannot convert value of type '[Images]?' to expected argument type 'String'

import SDWebImage

class AlbumCell: UITableViewCell {
    @IBOutlet weak var albumImageView: UIImageView!

    var album: Album? {
        didSet {
            guard let url = URL(string: album?.image) else { return }

            albumImageView.sd_setImage(with: url, completed: nil)
        }
    }
}

struct Album: Decodable {
    let name: String
    let image: [Images]
    let artist: String
}

struct Images: Decodable {
    let text: String
    let size: String

    enum CodingKeys: String, CodingKey {
        case text = "#text"
        case size
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
sk123
  • 580
  • 1
  • 8
  • 29
  • Per your previous question, if `#text` contains a url, make a `text: URL` type rather than a `String`. It will make things easier if you work with the correct types. – Ashley Mills Sep 30 '18 at 19:24

3 Answers3

2

Your Album struct contains a property image, which is an array of Images struct. In your URL(string: album?.image), URL constructor expects a string but you're providing an array of images.

You could do something like URL(string: album?.image[0].text) to get the string from images array. This will fetch the first image from the image array, you can change this index according to your needs to fetch the rest of the images.

Ehsan Saddique
  • 638
  • 4
  • 12
0

Cannot convert value of type '[Images]?' to expected argument type 'String'

Images is a struct, you need the url from the the struct. Such as

let urlstring:String = Images.text

But, your album image is an array of images, you need to pull those Image elements out using a for loop

for element in album.image { // need to consider each element in the array
     guard let url = URL(string: element.text) else {return} //then 
     albumImageView.sd_setImage(with: url, completed: nil)
Chris
  • 52
  • 8
-1

Convert array of image path into base64 encoded string.

Convert your ImagePaths (Strings) to NSData and from NSData back to string via base64EncodedStringWithOptions:

Here the code:

NSArray *recipeImages = [savedImagePath valueForKey:@"Image"];
NSMutableArray *mutableBase64StringsArray = @[].mutableCopy;

for (NSString *imagePath in recipeImages)
{
    NSData *imagePathData = [imagePath dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64ImagePath = [imagePathData base64EncodedStringWithOptions:0];
    [mutableBase64StringsArray addObject:base64ImagePath];
}