使用AFNetworking从服务器加载多个图像时,应用程序崩溃

时间:2015-11-30 15:14:03

标签: ios afnetworking

我已将AFNetworking库集成到我的应用程序中以从URL下载图像,当我们加载多个图像时,应用程序崩溃。

当我使用Xcode崩溃报告列表(Xcode - > Window - > Organizer - > Crashes - >我们的应用程序)分析我的代码时,它在AFNetworking库中显示这行代码的错误:

  

班级UIImageView+AFNetworking

     

方法名称setImageWithURLRequest

     

代码行[[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];

任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:4)

尝试以下代码

 NSURL *url = [NSURL URLWithString:@"YourImageURL"];

 NSURLRequest *request = [NSURLRequest requestWithURL:url];

 UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];

 __weak UIImageView *weakImageView = yourImageView;

 [weakImageView setImageWithURLRequest:request

                      placeholderImage:placeholderImage

                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                       weakImageView.image = image;

                                   } failure:nil];

答案 1 :(得分:1)

有许多关于图像下载的单元测试,以及3.0.0分支中的全新架构,其中包含正在下载多个图像的附加测试。

答案 2 :(得分:0)

https://github.com/Alamofire/Alamofire安装此广告

尝试以下代码将多个图像上传到服务器  代码:

 Alamofire.upload(
            .POST,
            urlString,
            multipartFormData: { multipartFormData in
                for image in images {
                    if let imageData = UIImageJPEGRepresentation(image, 0.1) {
                        multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
                    }
                }
            },
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON(completionHandler: { (response) -> Void in    
                        if response.result.error != nil {
                            print("Something is wrong while uploading images.")
                        } else {
                        }
                    })
                case .Failure(let encodingError):
                    print(encodingError)
                }
            }
        )