ios - Jsqmessageviewcontroller image upload and show on the screen is very slow touch is not responding -
whenever scroll chat page taking long time fetch images somtime not responding 1 me on this.
whenever scroll chat page taking long time fetch images somtime not responding 1 me on this.
- (id<jsqmessagedata>)collectionview:(jsqmessagescollectionview *)collectionview messagedataforitematindexpath:(nsindexpath *)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)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)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:@"post"]; 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 not made"); } }); }); [self dismissmodalviewcontrolleranimated:true]; } -(void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { webdata =[[nsmutabledata alloc]init]; } -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data{ [webdata appenddata:data]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)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 prefer time 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]; }]; }
it seems download images in main thread. change code download them asynchronously in background thread , update cells in main thread.
please check following links:
how asynchronously download , cache images without relying on third-party libraries
building concurrent user interfaces on ios
if not want write code use of following libraries: https://github.com/alamofire/alamofire, https://github.com/onevcat/kingfisher, http://asyncdisplaykit.org/
Comments
Post a Comment