我使用下面的代码将图像上传到我的服务器。代码运行良好,但指标并没有停止。我正在使用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");
}
}
我不知道问题出在哪里。警报消息出现但指示器未停止。我怎么能阻止它?
答案 0 :(得分:0)
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通常会更具响应性。