Skip to content

Instantly share code, notes, and snippets.

@0xhex
Created August 29, 2015 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xhex/214e3c04e3061a2508d0 to your computer and use it in GitHub Desktop.
Save 0xhex/214e3c04e3061a2508d0 to your computer and use it in GitHub Desktop.
image upload
-(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