【问题标题】:Image blank when using NSURLConnection but not when using dataWithContentsOfURL使用 NSURLConnection 时图像空白,但使用 dataWithContentsOfURL 时图像空白
【发布时间】:2011-06-28 19:38:22
【问题描述】:

当我异步使用NSURLConnection 来尝试获取带有我的图像数据的NSData 时,图像返回为空白,但是当我同步使用dataWithContentsOfURL 时,我没有任何问题,并且我正确获取了图像数据.我的异步方法会失败有什么原因吗?

这行得通:

NSData *data = [NSData dataWithContentsOfURL: url];
NSLog(@"TEST %@", data);
UIImage *map = [UIImage imageWithData:data];
mapView.image = map;

这不是:

//
//  MapHttpRequest.m
//  GTWeb
//
//  Created by Graphic Technologies on 6/21/11. 
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MapHttpRequest.h"

@implementation MapHttpRequest
@synthesize receivedData;
@synthesize dataString;
@synthesize vc;

- (void)request:(NSString *)url fromView:(UIViewController *) theVC
{
vc = theVC;
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];
NSLog(@"URL: %@", url);
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse             *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.

// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.

// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];

// inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere

[vc mapImageConnectionFinished:receivedData];

// release the connection, and the data object
[dataString release];
[connection release];
[receivedData release];
}

@end

【问题讨论】:

  • 查看使用NSURLConnection的代码会有所帮助。
  • @highlycaffeinated 我已经编辑了它,我不小心按了 Enter,它在我准备好之前就发布了。
  • 我看不到你在哪里填充你创建的 receivedData 对象。
  • 我确实发现返回的 MIME 类型是 text/html,即使 url 清楚地以 .jpg 结尾。 NSURLConnection 不是将其视为文本是否有某些原因?
  • 我将 receivedData 转换为 String 并打印出来,它是:

    Bad request

    。当我的浏览器和 dataWithContentsOfUrl 都没有问题使用相同的 url 返回图像时,为什么 NSURLConnection 会返回错误的请求?

标签: ios iphone asynchronous nsurlconnection


【解决方案1】:

您似乎开始查看 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html 上的指南,但没有读完 :)

您需要实现将接收有关正在接收的数据的信息的方法。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

在我提供的链接中都有描述。

【讨论】:

  • 不,我已经实现了使用 NSURLConnection 所需的所有四种方法,我只是发布了带有实际请求的 sn-p。如果您认为这可能是其中的问题,那么我也可以发布这些内容。
  • 是的,请发布完整的源代码:)
【解决方案2】:

根据您的代码,您没有安排连接运行:

[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];

您是否在代理的回调中添加了跟踪以确保它们被调用?

【讨论】:

  • 是的,我已经在回调中添加了跟踪信息,并且所有内容都被调用了。我不确定我是否需要执行 scheduleInRunLoop,因为我在其他地方使用相同的代码进行异步连接,该连接仅适用于文本。只是图像的 NSData 没有正确返回。
【解决方案3】:

原来它给了我一个错误的请求,因为有一个不可见的回车或空格。修剪它:

url = [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多