Created
August 29, 2015 16:13
-
-
Save 0xhex/214e3c04e3061a2508d0 to your computer and use it in GitHub Desktop.
image upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)UploadImageForMessage | |
{ | |
type=2; | |
NSString * str_ImageName = [[NSString alloc]initWithString:self.strImageName]; | |
NSString * str_ImageData = [[NSString alloc]initWithString:self.strImageData]; | |
NSLog(@"%@",str_ImageName); | |
NSUserDefaults *pref=[NSUserDefaults standardUserDefaults]; | |
msgCount++; | |
//// to local server | |
[[AppDelegate sharedAppDelegate] beginBackgroundUpdateTask]; | |
[pref setInteger:msgCount forKey:@"msgcount"]; | |
NSMutableDictionary *paraDict=[[NSMutableDictionary alloc]init]; | |
[paraDict setObject:str_ImageData forKey:Content]; | |
[paraDict setObject:str_ImageName forKey:Name]; | |
UIProgressView * progressView2 = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; | |
progressView2.progress = 0; | |
MAINPATCH | |
[progressView2 setFrame:CGRectMake(0, 0, self.view.frame.size.width, 25)]; | |
progressView2.alpha = 0; | |
[self.view addSubview:progressView2]; | |
[UIView animateWithDuration:0.5 | |
delay:0.3 | |
options: nil | |
animations:^{ | |
progressView2.alpha = 1; | |
} | |
completion:nil]; | |
patchEND | |
AFHTTPRequestOperationManager *manager = self.operationManager; | |
manager.requestSerializer = [AFHTTPRequestSerializer serializer]; | |
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; | |
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@",API_URL,Upload_Image] parameters:paraDict error:nil]; | |
AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseData) { | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
}]; | |
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseData) { | |
NSError* error; | |
NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseData | |
options:kNilOptions error:&error]; | |
int ii=[[response valueForKey:@"success"] intValue]; | |
MAINPATCH | |
[UIView animateWithDuration:0.5 | |
delay:0.3 | |
options: nil | |
animations:^{ | |
progressView2.alpha = 0; | |
} | |
completion:^(BOOL finished){ | |
[progressView2 removeFromSuperview]; | |
}]; | |
NSLog(@"operation completed %@",str_ImageName); | |
if(ii==1) | |
{ | |
type=2; | |
[self postImageMessageToServer:str_ImageName]; | |
} | |
else | |
{ | |
alert(@"Hata",@"Beklenmeyen bir hata oluştu tekrar deneyin",@"Tamam"); | |
} | |
patchEND | |
} | |
failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
MAINPATCH | |
[UIView animateWithDuration:0.5 | |
delay:0.3 | |
options: nil | |
animations:^{ | |
progressView2.alpha = 0; | |
} | |
completion:^(BOOL finished){ | |
[progressView2 removeFromSuperview]; | |
}]; | |
alert(@"Hata",@"Internet baglantinizi kontrol edin",@"Tamam"); | |
patchEND | |
}]; | |
[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { | |
MAINPATCH | |
double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite; | |
NSLog(@"progress updated(percentDone) : %f", percentDone); | |
progressView2.progress = percentDone; | |
patchEND | |
}]; | |
NSLog(@"operation waiting %@",str_ImageName); | |
[requestOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:nil]; | |
[self.operationManager.operationQueue addOperation:requestOperation]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment