在图像下载之前,connectionDidFinishLoading调用了吗?

时间:2015-06-16 02:26:59

标签: ios objective-c facebook uiimage nsurlconnection

我正在尝试检索Facebook个人资料图片,但是我无法检查图片何时下载?

首先我创建一个变量。

JNIEXPORT jint JNICALL Java_com_hanback_segment_SegmentActivity_SegmentControl(JNIEnv*  env, jobject thiz, jint data) {

JNIEXPORT jint JNICALL Java_com_hanback_segment_SegmentActivity_SegmentStateControl(JNIEnv* env, jobject thiz, jint data) {

比我开始连接。

@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"); } 几乎在图像下载之前立即调用。

connectionDidFinishLoading

奇怪的是,如果我两次调用此方法, - (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 :( } 不会返回null,它实际上会返回照片。那么为什么NSLog在图像从Facebook下载之前调用?

2 个答案:

答案 0 :(得分:0)

所以我不确定为什么在设置连接后会立即调用connectionDidFinishLoading,但我可以帮助您解决此问题。

试试这个:

-(UIImage *) getImageFromURL:(NSString *)fileURL {

    UIImage * result;

    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];

    return result;
}

其中fileURL是带有网址的字符串。

如果您想在发送请求后执行操作,请尝试以下操作:

-(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
        });

    });
}

让我知道它是怎么回事

答案 1 :(得分:0)

问题几乎肯定既不是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.实施将报告网络错误的代理; 3.如果图像转换失败,那么您显然没有收到图像,因此请停止并弄清楚您实际收到的内容。 (通常,如果服务器在完成您的请求时遇到问题,那么响应实际上是HTML或类似的东西,告诉您为什么它有问题。如果你不看它,你就会失明。 )

其次,您可以使用Charles(或类似内容)来观看网络连接。在模拟器上运行应用程序,然后在应用程序运行时观察网络连接。

第三,如果您仍然遇到问题,请创建MCVE。也就是说,我们不希望看到您的所有代码,但您应该创建一个最简单的示例来表示您描述的问题。不要让我们倾注大量的代码,而是让它尽可能地完全没有用。