任务完成后停止指示器

Stopping the Indicator after task finished

我正在使用下面的代码将图像上传到我的服务器。代码运行良好,但指标没有停止。我正在使用 Xcode 6 和 Objective-c,她是我的代码:

-(void) uploadImage
{
    NSData *imageData = UIImageJPEGRepresentation(self.createdImage.image,0.2);

    if (imageData != nil)
    {
        NSString * filenames = [NSString stringWithFormat:@"TextLabel"];
        NSLog(@"%@", filenames);

        NSString *urlString = @"http://myWebSite/sendVideo.php";

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@%@%@%@.mov\"\r\n", toSaveVideoLink, myString, FormattedDate, FormattedTime]] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"Response : %@",returnString);

        if([returnString isEqualToString:@"Success"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

            [alert show];

                [spinner stopAnimating];
               // [[UIApplication sharedApplication] endIgnoringInteractionEvents];
        }
        NSLog(@"Finish");
    }
}

不知道哪里出了问题。出现警报消息但指示器没有停止。我怎么能阻止它?

可能 spinner 没有引用您认为的 UIActivityIndicatorView。记录 spinner 的值并查看它包含的内容。还要确保它已正确添加到视图中,等等。如果不了解如何实例化微调器、如何将其添加到视图等,就无法判断。

但是,这里有一个更深层次的问题,您不应该执行同步请求。您正在这样发出请求:

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Response : %@",returnString);

if([returnString isEqualToString:@"Success"]) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

    [alert show];

    [spinner stopAnimating];
}

您应该异步执行此操作:

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    // this portion happens in the background

    NSString *returnString;

    if (data) {
        returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Response : %@",returnString);
        // do something with response
    } else {
        NSLog(@"Error: %@", error);
    }

    // dispatch UI update (and any model updates) back to the main queue

    dispatch_async(dispatch_get_main_queue(), ^{
        if([returnString isEqualToString:@"Success"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
        }
        NSAssert(spinner, @"Spinner is `nil`, but it should not be.");
        [spinner stopAnimating];
    });
}];
[task resume];

// do not do anything contingent upon the response here; the above runs asynchronously, so anything dependent upon the response must go in the block above

此外,如果您正在同步执行此块(这非常糟糕),我想知道您是否也在同步执行此后的任何操作(这会导致它无法及时响应微调器的停止方式)。同样,如果不看代码就无法分辨,但这是另一个可能的问题。最重要的是,只要确保您永远不会同步执行任何操作,您的 UI 通常会更加灵敏。