上传和下载时,进度条如何在图像上显示如何应用程序

时间:2016-04-07 06:46:12

标签: ios image chat grand-central-dispatch

您好我正在使用聊天应用程序,每件事情都正常。如果我向其他用户发送一个图像如何显示进度指示器,现在面临一个问题。在我的代码中,我所做的是 1)从galary中选择一个图像。 2)从他们上传到服务器我将获得Image Url。 3)我分配给聊天屏幕的网址。

如下图上传

-(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];

//    });

}

之后我会得到图片网址现在我将其分配给我的聊天屏幕方法,如下所示。我正在使用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];
           }];

工作正常的图像被添加到聊天屏幕但是在上传到服务器并从服务器下载以执行一些动画像whatapp请任何机构知道如何做它请帮助我

0 个答案:

没有答案