如何将图像数组转换为字符串
How to convert an array of images to a string
我可以从 api 中获取 json。除了 imageView
,我已经填充了所有其他视图。我有一组图像,它们是字符串 urls。我想下载这些图片。我目前正在使用 SDWebImage
。我需要将这个数组转换成一个字符串,这样我才能获取图像。我目前收到一条错误消息,因为 url 应该是字符串
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
}
}
将图片路径数组转换成base64编码的字符串。
通过 base64EncodedStringWithOptions 将您的 ImagePaths(字符串)转换为 NSData 并从 NSData 转换回字符串:
这里是代码:
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];
}
您的 Album
结构包含一个 属性 图像,它是 Images
结构的数组。
在您的 URL(string: album?.image)
、URL 构造函数中需要一个字符串,但您提供的是一组图像。
您可以执行类似 URL(string: album?.image[0].text)
的操作以从图像数组中获取字符串。这将从图像数组中获取第一张图像,您可以根据需要更改此索引以获取其余图像。
无法转换“[Images]”类型的值?到预期的参数类型 'String'
Images 是一个结构,您需要结构中的 url。如
let urlstring:String = Images.text
但是,您的相册图片是一个图片数组,您需要使用 for 循环将这些图片元素提取出来
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)
我可以从 api 中获取 json。除了 imageView
,我已经填充了所有其他视图。我有一组图像,它们是字符串 urls。我想下载这些图片。我目前正在使用 SDWebImage
。我需要将这个数组转换成一个字符串,这样我才能获取图像。我目前收到一条错误消息,因为 url 应该是字符串
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
}
}
将图片路径数组转换成base64编码的字符串。
通过 base64EncodedStringWithOptions 将您的 ImagePaths(字符串)转换为 NSData 并从 NSData 转换回字符串:
这里是代码:
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];
}
您的 Album
结构包含一个 属性 图像,它是 Images
结构的数组。
在您的 URL(string: album?.image)
、URL 构造函数中需要一个字符串,但您提供的是一组图像。
您可以执行类似 URL(string: album?.image[0].text)
的操作以从图像数组中获取字符串。这将从图像数组中获取第一张图像,您可以根据需要更改此索引以获取其余图像。
无法转换“[Images]”类型的值?到预期的参数类型 'String'
Images 是一个结构,您需要结构中的 url。如
let urlstring:String = Images.text
但是,您的相册图片是一个图片数组,您需要使用 for 循环将这些图片元素提取出来
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)