gpt4 book ai didi

ios - 如何从 URL 获取图像并放入 NSArray?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:16 24 4
gpt4 key购买 nike

我想从 URL 获取图像并将其放入 NSArray,但在下面的代码中不起作用,请帮助:

@interface DataViewController ()

@property (readonly, strong, nonatomic) NSArray *picData;

@end

@implementation DataViewController

- (void)viewDidLoad {

NSString *imageUrlStringAA=@"http://aaa.net/AA.png";
NSString *imageUrlStringBB=@"http://aaa.net/BB.png";

NSURL *urlAA=[NSURL URLWithString:imageUrlStringAA];
NSURL *urlBB=[NSURL URLWithString:imageUrlStringBB];

NSData *dataAA=[[NSData alloc] initWithContentsOfURL:urlAA];
NSData *dataBB=[[NSData alloc] initWithContentsOfURL:urlBB];

UIImage *imageAA=[UIImage imageWithData:dataAA];
UIImage *imageBB=[UIImage imageWithData:dataBB];

_picData = [NSArray arrayWithObjects:imageAA, imageBB, nil];

感谢任何想法,谢谢。

最佳答案

首先从 http 协议(protocol)下载任何东西,然后在 info.plist 中添加以下语句

Allow http protocol

现在试试这段代码:

@interface DataViewController ()

@property (readonly, strong, nonatomic) NSArray *picData;

@end

@implementation DataViewController

- (void)viewDidLoad {
NSString *imageUrlStringAA=@"http://www.hdwallpapers.in/download/antelope_canyon-360x640.jpg";
NSString *imageUrlStringBB=@"http://www.hdwallpapers.in/download/vegeta_dragon_ball_super-360x640.jpg";

NSURL *urlAA=[NSURL URLWithString:imageUrlStringAA];
NSURL *urlBB=[NSURL URLWithString:imageUrlStringBB];

NSData *dataAA=[[NSData alloc] initWithContentsOfURL:urlAA];
NSData *dataBB=[[NSData alloc] initWithContentsOfURL:urlBB];

UIImage *imageAA=[UIImage imageWithData:dataAA];
UIImage *imageBB=[UIImage imageWithData:dataBB];
_picData = [NSArray arrayWithObjects:imageAA, imageBB, nil];
}

关于ios - 如何从 URL 获取图像并放入 NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com