每当我滚动我的聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗?
每当我滚动聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗?
- (id<JSQMessageData>)collectionViewJSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPathNSIndexPath *)indexPath {
JSQMessage *messageForRow;
NSString * chart = [[DataArray objectAtIndex:indexPath.row] objectForKey"Message"];
NSString * imageurl = [[DataArray objectAtIndex:indexPath.row] objectForKey"media"];
if (chart == nil)
{
NSURL *imageURL = [NSURL URLWithString:imageurl];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image1 = [UIImage imageWithData:imageData];
JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:image1];
messageForRow = [[JSQMessage alloc] initWithSenderId:[[DataArray objectAtIndex:indexPath.row] objectForKey"UserId"] senderDisplayName:[[DataArray objectAtIndex:indexPath.row] objectForKey"Name"] date:[NSDate distantPast] media:item] ;
}
else
{
messageForRow = [[JSQMessage alloc] initWithSenderId:[[DataArray objectAtIndex:indexPath.row] objectForKey"UserId"] senderDisplayName:[[DataArray objectAtIndex:indexPath.row] objectForKey"Name"] date:[NSDate distantPast] text: [[DataArray objectAtIndex:indexPath.row] objectForKey"Message"]] ;
}
return messageForRow;
}
- (void)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)info
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
img = [info valueForKey:UIImagePickerControllerOriginalImage];
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];
NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString"." withString""]stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];
NSString *finalUniqueImageNAme = [cleanedString2 stringByAppendingString:@".jpg"];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSString *urlString = @"http://192.168.1.92/Abdul/IOS/Chat/upload/upload_file.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"OST"];
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=\"file\"; filename=\"%@\"\r\n",finalUniqueImageNAme] 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(@"Successfully uploaded");
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
[self dismissModalViewControllerAnimated:true];
}
else
{
NSLog(@"Connection could not be made");
}
});
});
[self dismissModalViewControllerAnimated:true];
}
-(void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response
{
webdata =[[NSMutableData alloc]init];
}
-(void)connectionNSURLConnection *)connection didReceiveDataNSData *)data{
[webdata appendData:data];
}
-(void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error
{
NSLog(@"%@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// NSString *responseString = [[NSString alloc]initWithData:webdata encoding:NSUTF8StringEncoding];
dic=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
// NSLog( @"Success %@",dic);
res = [dic objectForKey:@"url"];
NSLog(@"%@",res);
NSString * sta = [dic objectForKey:@"Success"];
if (![sta isEqualToString:@"1"])
{
return ;
}
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSString * date = [dateFormatter stringFromDate:[NSDate date]];
NSString *urlstr1=[@"https://popping-torch-4696.firebaseio.com/" stringByAppendingString:recvStr];
Firebase *myRootRef = [[Firebase alloc] initWithUrl:urlstr1];
NSString *dateStr=[NSString stringWithFormat:@"%@",date];
[[myRootRef childByAutoId ] setValue:@{@"UserId" : str1,@"Name":str2,@"media":res , @"date":dateStr,}];
[myRootRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
[self.collectionView reloadData];
}];
}
Best Answer-推荐答案 strong>
您似乎在主线程中下载图像。更改代码以在后台线程中异步下载它们,然后在主线程中更新单元格。
请检查以下链接:
How to Asynchronously Download and Cache Images without Relying on Third-Party Libraries
Building Concurrent User Interfaces on iOS
如果您不想自己编写代码,可以使用以下一些库:https://github.com/Alamofire/Alamofire , https://github.com/onevcat/Kingfisher , http://asyncdisplaykit.org/
关于ios - jsqmessageviewcontroller 图片上传并在屏幕上显示很慢 触摸无响应,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/36713785/
|