gpt4 book ai didi

iOS : How to upload an image from a URL to Parse?

转载 作者:行者123 更新时间:2023-11-29 12:21:58 26 4
gpt4 key购买 nike

我可以使用可用的方法在解析时上传图像(以 PFFile 的形式)。

NSData *data = ... ;
PFFile *file = [PFFile fileWithData:data];
[file saveInBackground];

有没有更好的方法直接从url上传图片进行解析?

更新:我发现的错误方法是,

NSURL 获取图像到 NSData - 不知道正确的方法,但这是可行的。像这样,

PFFile *file = [PFFile fileWithData:[NSData dataWithContentsOfURL:urlObj]];
[file saveInBackground];

最佳答案

我认为问题在于如何最好地获取要传递给解析的数据。你说得对,dataWithContentsOfUrl 是一种糟糕的方式,因为它会在获取期间阻塞主线程。

这是一种基于 block 的异步获取数据的方法:

NSURL *url = [NSURL URLWithString:@"http:// ..."];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error) {
PFFile *file = [PFFile fileWithData:data];
[file saveInBackground];
}
}];

关于iOS : How to upload an image from a URL to Parse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350307/

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