UIScrollView在图像10之后滑动时崩溃应用程序

时间:2016-05-11 12:14:24

标签: ios swift uiscrollview

我已经创建了一个图片库,我已经完成了所有使用低分辨率图像的工作,因为据我所知,如果你使用过多的高分辨率,它会崩溃我的应用程序。我现在面临的问题是,当用户点击图片时,在刷了9-10张图片后,它会崩溃应用程序。阻止这种情况发生的最佳做法是什么?

这是我的加载页面功能,我相信这个问题可能是:

func loadPage(page: Int) {

        if page < 0 || page >= arrGallery.count {
            // If it's outside the range of what you have to display, then do nothing
            return
        }

        if let page = pageViews[page] {
            // Do nothing. The view is already loaded.
        } else {
            var frame = scrollView.bounds
            frame.origin.x = frame.size.width * CGFloat(page)
            frame.origin.y = 0.0

            let rowData: NSDictionary = arrGallery[page] as! NSDictionary
            let urlString: String = rowData["pic"] as! String
            let imgURL = NSURL(string: urlString)
            // If this image is already cached, don't re-download
            if let img = imageCache[urlString] {
                let newPageView = UIImageView(image: img)
                newPageView.contentMode = .ScaleAspectFit
                newPageView.frame = frame
                scrollView.addSubview(newPageView)


                pageViews[page] = newPageView
            }
            else {
                // The image isn't cached, download the img data
                // We should perform this in a background thread
                let request: NSURLRequest = NSURLRequest(URL: imgURL!)
                let mainQueue = NSOperationQueue.mainQueue()
                NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
                    if error == nil {
                        // Convert the downloaded data in to a UIImage object
                        let image = UIImage(data: data!)
                        // Store the image in to our cache

                        dispatch_async(dispatch_get_main_queue(), {
                            self.imageCache[urlString] = image
                            // Update the cell
                            let newPageView = UIImageView(image: image)
                            newPageView.contentMode = .ScaleAspectFit
                            newPageView.frame = frame
                            self.scrollView.addSubview(newPageView)

                            // 4
                            self.pageViews[page] = newPageView
                        })
                    }
                    else {
                        print("Error: \(error!.localizedDescription)")
                    }
                })
            }

        }

我收到的崩溃是:

  

2016-05-11 13:28:58.743 The Britannia [19024:7909175]收到记忆   警告。

谢谢!

更新:

我已经使用SDWebImage在图像上实现了延迟加载,你的答案和新代码是:

func loadPage(page: Int) {

        if page < 0 || page >= arrGallery.count {
            // If it's outside the range of what you have to display, then do nothing
            return
        }

        if let page = pageViews[page] {
            // Do nothing. The view is already loaded.
        } else {
            var frame = scrollView.bounds
            frame.origin.x = frame.size.width * CGFloat(page)
            frame.origin.y = 0.0

            let rowData: NSDictionary = arrGallery[page] as! NSDictionary
            let urlString: String = rowData["pic"] as! String
            let imgURL = NSURL(string: urlString)
            // If this image is already cached, don't re-download
            if let img = imageCache[urlString] {
                let newPageView = UIImageView(image: img)
                newPageView.contentMode = .ScaleAspectFit
                newPageView.frame = frame
                scrollView.addSubview(newPageView)


                pageViews[page] = newPageView
            }
            else {


                SDWebImageManager.sharedManager().downloadImageWithURL(imgURL, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in

                    if error == nil {

                     //   let image = UIImage(data: data!)
                        //On Main Thread
                        dispatch_async(dispatch_get_main_queue()){
                            self!.imageCache[urlString] = image
                                                        // Update the cell
                                                        let newPageView = UIImageView(image: image)
                                                        newPageView.contentMode = .ScaleAspectFit
                                                        newPageView.frame = frame
                                                        self!.scrollView.addSubview(newPageView)

                                                        // 4
                                                        self!.pageViews[page] = newPageView
                        }
                    }
                    else {
                        print("Error: \(error!.localizedDescription)")
                    }
                    })
            }
            return

        }

    }

谢谢!

0 个答案:

没有答案