ios - jsqmessageviewcontroller 图片上传并在屏幕上显示很慢 触摸无响应
<p><p>每当我滚动我的聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗? </p>
<p>每当我滚动聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗?</p>
<pre><code>- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
JSQMessage *messageForRow;
NSString * chart = [ objectForKey:@"Message"];
NSString * imageurl = [ objectForKey:@"media"];
if (chart == nil)
{
NSURL *imageURL = ;
NSData *imageData = ;
UIImage *image1 = ;
JSQPhotoMediaItem *item = [ initWithImage:image1];
messageForRow = [ initWithSenderId:[ objectForKey:@"UserId"] senderDisplayName:[ objectForKey:@"Name"] date: media:item] ;
}
else
{
messageForRow = [ initWithSenderId:[ objectForKey:@"UserId"] senderDisplayName:[ objectForKey:@"Name"] date: text: [ objectForKey:@"Message"]] ;
}
return messageForRow;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
img = ;
NSDate *currentDate = [ init];
NSDateFormatter *dateFormatter = [ init];
;
NSString *localDateString = ;
NSString* cleanedString = [stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *cleanedString2 = ;
NSString *finalUniqueImageNAme = ;
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSString *urlString = @"http://192.168.1.92/Abdul/IOS/Chat/upload/upload_file.php";
NSMutableURLRequest *request = [ init];
];
;
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = ;
;
NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
;
NSData *returnData = ;
NSString *returnString = [ initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Successfully uploaded");
NSURLConnection *conn = [ initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
;
}
else
{
NSLog(@"Connection could not be made");
}
});
});
;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
webdata =[init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// NSString *responseString = [initWithData:webdata encoding:NSUTF8StringEncoding];
dic=;
//NSLog( @"Success %@",dic);
res = ;
NSLog(@"%@",res);
NSString * sta = ;
if (!)
{
return ;
}
NSDateFormatter *dateFormatter=[ init];
;
// or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSString * date = ];
NSString *urlstr1=[@"https://popping-torch-4696.firebaseio.com/" stringByAppendingString:recvStr];
Firebase *myRootRef = [ initWithUrl:urlstr1];
NSString *dateStr=;
[ setValue:@{@"UserId" : str1,@"Name":str2,@"media":res , @"date":dateStr,}];
[myRootRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
;
}];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您似乎在主线程中下载图像。更改代码以在后台线程中异步下载它们,然后在主线程中更新单元格。</p>
<p>请检查以下链接:</p>
<p> <a href="http://sweettutos.com/2015/12/31/swift-how-to-asynchronously-download-and-cache-images-without-relying-on-third-party-libraries/" rel="noreferrer noopener nofollow">How to Asynchronously Download and Cache Images without Relying on Third-Party Libraries</a> </p>
<p> <a href="https://developer.apple.com/videos/play/wwdc2012/211/" rel="noreferrer noopener nofollow">Building Concurrent User Interfaces on iOS</a> </p>
<p>如果您不想自己编写代码,可以使用以下一些库:<a href="https://github.com/Alamofire/Alamofire" rel="noreferrer noopener nofollow">https://github.com/Alamofire/Alamofire</a> , <a href="https://github.com/onevcat/Kingfisher" rel="noreferrer noopener nofollow">https://github.com/onevcat/Kingfisher</a> , <a href="http://asyncdisplaykit.org/" rel="noreferrer noopener nofollow">http://asyncdisplaykit.org/</a> </p></p>
<p style="font-size: 20px;">关于ios - jsqmessageviewcontroller 图片上传并在屏幕上显示很慢 触摸无响应,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36713785/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36713785/
</a>
</p>
页:
[1]