0

Hi I am working with Chat app every thing is working fine. Now facing one issue if i send one image to other user how to show progress indicator. In my code what i have done is 1) pick one image from galary. 2) upload to server from their i will get Image Url. 3) That url I am assigning to chat screen.

like below uploading image

-(void)uploadImage
{
//    dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
//    dispatch_async(myQueue, ^{

imageView.image = [mediaInfo objectForKey:UIImagePickerControllerEditedImage];
self.indicatorImage=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.indicatorImage setColor:[UIColor blackColor]];
//    [self.indicatorImage startAnimating];
self.indicatorImage.center = CGPointMake(CGRectGetMidX(msgCell.mediaImageView.bounds), CGRectGetMidY(msgCell.mediaImageView.bounds));
[msgCell.mediaImageView addSubview:self.indicatorImage];
 [self.indicatorImage startAnimating];
NSURL *url=[NSURL URLWithString:[[NSString stringWithFormat:@"http://192.168.0.24/openfire/index.php?"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// create request
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[theRequest setHTTPShouldHandleCookies:NO];
[theRequest setTimeoutInterval:30];
[theRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString *test = @"upload_image"; //Image name if you want a custom name
NSMutableData *body=[[NSMutableData alloc] init];
// add image data
imageData= UIImageJPEGRepresentation(imageView.image, 1.0);
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if (imageData)
    [body appendData:[[NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"",test,@".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if (imageData)
    [body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[theRequest setURL:url];
NSURLResponse *responceData;
returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&responceData error:nil];
NSError *serializeError = nil;
NSDictionary *jsonData = [NSJSONSerialization
                          JSONObjectWithData:returnData
                          options:NSJSONReadingMutableContainers
                          error:&serializeError];
NSLog(@"print response after image post : %@",jsonData);
imgURL=[jsonData objectForKey:@"download"];
_data = [imgURL dataUsingEncoding:NSUTF8StringEncoding];

//    });

}

after that i will get the image url Now I am assigning that to my chat screen method like below . I am using SDwebImage Library

 __weak typeof(self)weakSelf = self;
   [msgCell.mediaImageView sd_setImageWithURL:[NSURL         URLWithString:urlString] placeholderImage:nil options:SDWebImageCacheMemoryOnly | SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
   [msgCell.mediaImageView updateImageDownloadProgress:(CGFloat)receivedSize/expectedSize];
           } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
               [msgCell.mediaImageView reveal];
           }];

It is working fine image is added to chat screen but while uploading to server and downloading from server ineed to perform some animation like whatapp please any body knows how to do it please help me

Bittoo
  • 579
  • 7
  • 21

0 Answers0