菜鸟教程小白 发表于 2022-12-13 15:58:28

ios - jsqmessageviewcontroller 图片上传并在屏幕上显示很慢 触摸无响应


                                            <p><p>每当我滚动我的聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗? </p>

<p>每当我滚动聊天页面时,它需要很长时间才能获取图像,有时它没有响应,有人可以帮助我吗?</p>

<pre><code>- (id&lt;JSQMessageData&gt;)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {



      JSQMessage *messageForRow;


      NSString * chart = [ objectForKey:@&#34;Message&#34;];
      NSString * imageurl = [ objectForKey:@&#34;media&#34;];


      if (chart == nil)
      {
            NSURL *imageURL = ;


            NSData *imageData = ;


            UIImage *image1 = ;


            JSQPhotoMediaItem *item = [ initWithImage:image1];
            messageForRow = [ initWithSenderId:[ objectForKey:@&#34;UserId&#34;] senderDisplayName:[ objectForKey:@&#34;Name&#34;] date: media:item] ;


      }
      else
      {
      messageForRow = [ initWithSenderId:[ objectForKey:@&#34;UserId&#34;] senderDisplayName:[ objectForKey:@&#34;Name&#34;] date: text: [ objectForKey:@&#34;Message&#34;]] ;
      }




      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:@&#34;:&#34; withString:@&#34;&#34;];
    NSString *cleanedString2 = ;
    NSString *finalUniqueImageNAme = ;



    NSData *imageData = UIImageJPEGRepresentation(img, 90);
    NSString *urlString = @&#34;http://192.168.1.92/Abdul/IOS/Chat/upload/upload_file.php&#34;;

    NSMutableURLRequest *request = [ init];
    ];
    ;
    NSString *boundary = @&#34;---------------------------14737809831466499882746641449&#34;;
    NSString *contentType = ;
    ;

    NSMutableData *body = ;
    dataUsingEncoding:NSUTF8StringEncoding]];
    dataUsingEncoding:NSUTF8StringEncoding]];
    ];
    ];
    dataUsingEncoding:NSUTF8StringEncoding]];
    ;

    NSData *returnData = ;
    NSString *returnString = [ initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@&#34;Successfully uploaded&#34;);




    NSURLConnection *conn = [ initWithRequest:request delegate:self];
    if(conn)
    {
      NSLog(@&#34;Connection Successful&#34;);
         ;
    }
    else
    {
      NSLog(@&#34;Connection could not be made&#34;);
    }

      });
    });
    ;
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    webdata =[init];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    ;

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{


    NSLog(@&#34;%@&#34;,error);

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//   NSString *responseString = [initWithData:webdata encoding:NSUTF8StringEncoding];

    dic=;
    //NSLog( @&#34;Success %@&#34;,dic);

   res = ;

    NSLog(@&#34;%@&#34;,res);
    NSString * sta = ;
    if (!)
    {
      return ;

    }


    NSDateFormatter *dateFormatter=[ init];
    ;
    // or @&#34;yyyy-MM-dd hh:mm:ss a&#34; if you prefer the time with AM/PM

    NSString * date = ];


    NSString *urlstr1=[@&#34;https://popping-torch-4696.firebaseio.com/&#34; stringByAppendingString:recvStr];

    Firebase *myRootRef = [ initWithUrl:urlstr1];

    NSString *dateStr=;

    [ setValue:@{@&#34;UserId&#34; : str1,@&#34;Name&#34;:str2,@&#34;media&#34;:res , @&#34;date&#34;: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]
查看完整版本: ios - jsqmessageviewcontroller 图片上传并在屏幕上显示很慢 触摸无响应