• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:58:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

- (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-推荐答案


您似乎在主线程中下载图像。更改代码以在后台线程中异步下载它们,然后在主线程中更新单元格。

请检查以下链接:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap