【发布时间】:2016-07-04 03:05:02
【问题描述】:
我有一个超级简单的代码,可以从网上下载一张图片。它在 iOS 8 中运行良好,但现在我在带有 iOS 9 的 Xcode 7.3 中不断收到此错误。这是一个错误吗?
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSError *error;
NSString *data2URL = @"http://cdn.minecraftpocket-servers.com/images/flags/Brazil.png";
NSURL *url = [NSURL URLWithString:data2URL];
NSString *imgFileNameStr = [data2URL lastPathComponent];
NSData *data2 = [NSData dataWithContentsOfURL:url options: NSDataReadingUncached error: &error];
if (error)
NSLog(@"Download error: %@",error);
//check
if (data2 == NULL)
{
NSLog(@"DATA IS NULL");
}
else
{
NSLog(@"DATA IS NOT NULL");
}
//saving file
NSString* fullPathToFile2 = [documentsPath stringByAppendingPathComponent:imgFileNameStr];
BOOL success = [data2 writeToFile:fullPathToFile2 atomically:NO];
NSLog(@"Success = %d ...", success);
错误:
2016-07-03 23:00:36.963 014-test-proj[15404:419151] Download error: Error Domain=NSCocoaErrorDomain Code=256 "The file “Brazil.png” couldn’t be opened." UserInfo={NSURL=http://cdn.minecraftpocket-servers.com/images/flags/Brazil.png}
2016-07-03 23:00:36.963 014-test-proj[15404:419151] DATA IS NULL
2016-07-03 23:00:36.964 014-test-proj[15404:419151] Success = 0 ...
我已经检查并确保了我的 plist 文件的应用程序安全性。
有人遇到了同样的问题HERE。我错过了什么?
更新: 如果我使用这个链接,代码就可以工作......
NSString *data2URL =@"https://upload.wikimedia.org/wikipedia/commons/1/1e/Large_Siamese_cat_tosses_a_mouse.jpg";
我不明白。为什么它会与另一个有问题。
【问题讨论】:
标签: ios objective-c xcode