0

例如,我的 ftp 服务器上有 100 个 JSON 文件和相应的 100 个图像。

1.加载JSON没有错误(我希望)

NSString *recipesPath = @"ftp://.../recipes/";
NSString *recipeFileName = [NSString stringWithFormat:@"00001.json", recipeCode];
NSString *recipeFileFullPath = [recipesPath stringByAppendingString:recipeFileName];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:recipeFileFullPath]];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSError *error = nil;
NSDictionary *recipeDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

2.但是图片无法加载

NSString *recipeImageName = @"recipeImages/00001-1.jpg";
NSString *recipeImageFullPath = [recipesPath stringByAppendingString:recipeImageName];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:recipeImageFullPath]];
if (request) {
   NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   if (responseData) {
       UIImage *image = [UIImage imageWithData:responseData];
   }
}

responseData几乎总是nil

可能有其他方法吗?

所有这些代码都在我执行的MyMethodNSOperationQueue

operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(MyMethod) object:nil];
[operationQueue addOperation:operation];

编辑:图像尺寸不大 - 从 50 到 100 kbyte

编辑:图像文件扩展名会影响下载过程吗?

4

2 回答 2

0

你可以试试这个:

NSURL *url = [NSURL URLWithString:recipeImageFullPath];
NSData *data = [NSData alloc] initWithContentsOfURL:url];
于 2013-04-30T10:22:24.697 回答
0

请尝试测试它:

NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request  returningResponse:&urlResponse error:&requestError];
/* Return Value
  The downloaded data for the URL request. Returns nil if a connection could not be created or if  the download fails.
*/
 if (responseData == nil) {
// Check for problems
   if (requestError != nil) {
    ...
   }
}
else {
 // Data was received.. continue processing
}
于 2013-04-30T10:28:58.267 回答