Ios ConnectionDiFinishLoading调用是否在图像下载之前?
我正在尝试检索Facebook个人资料图片,但是我无法检查图片何时下载 首先,我创建一个变量Ios ConnectionDiFinishLoading调用是否在图像下载之前?,ios,objective-c,facebook,uiimage,nsurlconnection,Ios,Objective C,Facebook,Uiimage,Nsurlconnection,我正在尝试检索Facebook个人资料图片,但是我无法检查图片何时下载 首先,我创建一个变量 @property (strong, nonatomic) NSMutableData *imageData; 然后我开始连接 -(void)getUserPicture { //Grab user profile picture imageData = [[NSMutableData alloc] init]; // the image will be loaded in here NSStrin
@property (strong, nonatomic) NSMutableData *imageData;
然后我开始连接
-(void)getUserPicture {
//Grab user profile picture
imageData = [[NSMutableData alloc] init]; // the image will be loaded in here
NSString *urlString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", userId];
NSMutableURLRequest *urlRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
if (!urlConnection) NSLog(@"Failed to download picture");
}
之后,我尝试检查何时完成,以便将文件上载到后端,但我的问题是connectiondifinishload
几乎在图像下载之前立即调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
imageData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[imageData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
userPicture = [UIImage imageWithData:imageData];
NSLog(@"%@",userPicture); //this returns null :(
}
奇怪的是,如果我调用这个方法两次,
NSLog
不会返回null,它实际上会返回照片。那么,为什么在从Facebook下载图像之前,connectiondFinishedLoading
会呼叫 所以我不确定为什么在您设置连接后会立即调用ConnectiondFinishLoading,但我可以帮助您解决这个问题
试试这个:
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
其中fileURL是一个带有url的字符串
如果要在发送请求后执行操作,请尝试以下操作:
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
dispatch_async(dispatch_get_main_queue(), ^{
//code for operation after download
});
});
}
让我知道它是如何进行的,因此我不确定为什么在您设置连接后会立即调用ConnectiondFinishLoading,但我可以帮助您解决此问题 试试这个:
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
其中fileURL是一个带有url的字符串
如果要在发送请求后执行操作,请尝试以下操作:
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
dispatch_async(dispatch_get_main_queue(), ^{
//code for operation after download
});
});
}
让我知道它是如何运行的问题几乎肯定既不是
NSURLConnection
也不是Facebook API,而是您如何调用它。但是,您的问题没有包含足够的信息,我们无法对其进行诊断
因此,首先,扩展方法以包含更多诊断信息,例如:
// check the response header when we receive the response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
imageData = [NSMutableData data];
// if `statusCode` is not 200, show us what it was ...
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
int statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"Status code was %ld, but should be 200.", (long)statusCode);
NSLog(@"response = %@", response);
}
}
}
// make sure to detect and report any errors
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", error);
}
// when you're done, if we fail to create `UIImage`, then it obviously
// wasn't an image, so let's see what it was.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
userPicture = [UIImage imageWithData:imageData];
// if not an image, then ...
if (!userPicture) {
NSString *responseString = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
if (responseString) {
// if it was a string, show it to us ...
NSLog(@"did not receive image: responseString = %@", responseString);
} else {
// if neither a string nor image data, then what was it ...
NSLog(@"did not receive image: imageData = %@", imageData);
}
}
// By the way, I'd expect to see you do something here to update the UI with the image;
// all of these delegate methods were called asynchronously, so you have
// to do something here that triggers the update of the UI, e.g.
//
// self.imageView.image = userPicture
}
顺便说一句,我键入上述内容时没有使用Xcode的语法检查等功能,因此如果出现一些错误,请不要感到惊讶。但不要太担心实际的代码,而是关注这三个诊断支柱:1。查看响应头并确保它们正常,不报告一些非200状态代码;2.实现将报告网络错误的委托;三,。如果图像转换失败,那么您显然没有收到图像,所以请停止并找出您实际收到的图像。(通常情况下,如果服务器无法满足您的请求,响应实际上是HTML或类似的东西,它会告诉您为什么它会出现问题。如果您不查看它,您就瞎了眼。)
其次,您可以使用(或类似的方式)查看网络连接。在模拟器上运行应用程序,然后在应用程序运行时观察网络连接
第三,如果你仍然有问题,创建一个新的解决方案。也就是说,我们不希望看到所有的代码,但是您应该创建一个最简单的示例来说明您描述的问题。不要要求我们倾注大量的代码,而是尽可能地将其完全简化。问题几乎肯定既不是
NSURLConnection
也不是Facebook API,而是您如何调用它。但是,您的问题没有包含足够的信息,我们无法对其进行诊断
因此,首先,扩展方法以包含更多诊断信息,例如:
// check the response header when we receive the response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
imageData = [NSMutableData data];
// if `statusCode` is not 200, show us what it was ...
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
int statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"Status code was %ld, but should be 200.", (long)statusCode);
NSLog(@"response = %@", response);
}
}
}
// make sure to detect and report any errors
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", error);
}
// when you're done, if we fail to create `UIImage`, then it obviously
// wasn't an image, so let's see what it was.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
userPicture = [UIImage imageWithData:imageData];
// if not an image, then ...
if (!userPicture) {
NSString *responseString = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
if (responseString) {
// if it was a string, show it to us ...
NSLog(@"did not receive image: responseString = %@", responseString);
} else {
// if neither a string nor image data, then what was it ...
NSLog(@"did not receive image: imageData = %@", imageData);
}
}
// By the way, I'd expect to see you do something here to update the UI with the image;
// all of these delegate methods were called asynchronously, so you have
// to do something here that triggers the update of the UI, e.g.
//
// self.imageView.image = userPicture
}
顺便说一句,我键入上述内容时没有使用Xcode的语法检查等功能,因此如果出现一些错误,请不要感到惊讶。但不要太担心实际的代码,而是关注这三个诊断支柱:1。查看响应头并确保它们正常,不报告一些非200状态代码;2.实现将报告网络错误的委托;三,。如果图像转换失败,那么您显然没有收到图像,所以请停止并找出您实际收到的图像。(通常情况下,如果服务器无法满足您的请求,响应实际上是HTML或类似的东西,它会告诉您为什么它会出现问题。如果您不查看它,您就瞎了眼。)
其次,您可以使用(或类似的方式)查看网络连接。在模拟器上运行应用程序,然后在应用程序运行时观察网络连接
第三,如果你仍然有问题,创建一个新的解决方案。也就是说,我们不希望看到所有的代码,但是您应该创建一个最简单的示例来说明您描述的问题。不要要求我们将大量的代码倾注其中,而是尽可能将其完全简化。将您的
userPicture=[UIImage-imageWithData:imageData]代码>在您的didReceiveData中。@SASmith不,这是一个非常糟糕的主意。除非图像很小,否则您很可能还没有收到当时的所有数据。@Rob,我不确定它是否在下载之前调用。我所知道的是,我的NSLOG在方法第一次调用时返回null,而不是我回忆图像加载的方法??同样,在didReceiveResponse
中,查看nsurresponse
对象。值得注意的是,如果状态代码不是200,则表示您有服务器问题(或导致服务器错误的错误请求)。最后,您是否也实现了connection:didFailWithError:
delegate方法?@Rob,首先这个方法在我的ViewController类中,我想我的所有其他类都是从这个类中派生出来的,所以每次为我的任何ViewController加载视图时,都会调用这个方法。我不确定这是否有什么不同。第二,当第一次加载该方法时,控制台将返回此值。然后,如果我加载了另一个视图控制器,则会再次调用该方法,并且控制台会返回该方法。奇怪的是,只要它被调用两次而不是一次,它就会给我一个很好的图像。把你的userPicture=[UIImage-imageWithData:imageData]代码>在您的didReceiveData中。@SASmith不,这是一个非常糟糕的主意。除非图像很小,否则您很可能还没有收到当时的所有数据。@Rob,我不确定它是否在下载之前调用。我所知道的是,我的NSLOG在方法第一次调用时返回null,而不是如果我