在集合视图中调用reloadData()后,未显示已保存的UIImages

时间:2016-07-30 17:41:29

标签: ios swift image uiimage uicollectionview

enter image description here在调用realoadData()后,我的集合视图中有一些已保存的图像,只显示第一个,其余的不在那里。

以下是我的collectionView中的一些代码: 我从directoryPath加载图像。 当他们来自相机时我会保存它们。

enter override func viewDidLoad() {

    self.collectionView?.delegate = self
    collectionView!.dataSource = self
    print("loaded")
    self.prepareUI()
    images = []


    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    // Get the Document directory path
    let documentDirectorPath:String = paths[0]
    // Create a new path for the new images folder
    imagesDirectoryPath = documentDirectorPath.stringByAppendingString("/ImagePicker")
    var objcBool:ObjCBool = true
    let isExist = NSFileManager.defaultManager().fileExistsAtPath(imagesDirectoryPath, isDirectory: &objcBool)
    // If the folder with the given path doesn't exist already, create it
    if isExist == false{
        do{
            try NSFileManager.defaultManager().createDirectoryAtPath(imagesDirectoryPath, withIntermediateDirectories: true, attributes: nil)
        }catch{
            print("Something went wrong while creating a new folder")
        }
    }

if(names.isEmpty == false)
{
  refreshTable()
}




}
deinit{
    self.removeObservers()
}

func refreshTable(){
    do{

        images.removeAll()
        titles = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(imagesDirectoryPath)
        for image in titles{
            let data = NSFileManager.defaultManager().contentsAtPath(imagesDirectoryPath.stringByAppendingString("/\(image)"))
            let image = UIImage(data: data!)
            images.append(image!)
        }
        self.collectionView?.reloadData()

    }catch{
        print("Error")
    }
}


override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FlickrPhotoCell




if(searchBarActive == true)
{
    cell.nameLabel.text = researchResults[indexPath.item]
    cell.imageView.image = sFunc_imageFixOrientation(resultImages[indexPath.item])

}
else
{

cell.nameLabel.text = names[indexPath.item]
cell.imageView.image = sFunc_imageFixOrientation(images[indexPath.item])

}

cell.nameLabel.hidden = false

cell.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width/2  , UIScreen.mainScreen().bounds.width/2)

 cell.imageView.frame = cell.frame



cell.backgroundColor = UIColor.clearColor()
cell.layer.borderColor = themeColor.CGColor
cell.layer.borderWidth = 4
cell.layer.cornerRadius = CGFloat(10.0)
print(images)






return cell

}

func filterContentForSearchText(searchText:String){
    self.researchResults = names.filter({ (text:String) -> Bool in
        return text.containsString(searchText)
    })
}

//Suche Fotos für gesuchte items
func getSearchedFotos()
{
    print(researchResults)
    if resultImages.isEmpty == false
    {
        resultImages.removeAll()
    }
    for (var i = 0; i <= names.count - 1; i += 1)
    {
        for (var j = 0; j <= researchResults.count - 1; j += 1)
        {

            if(researchResults[j] == names[i])
            {
                resultImages.append(images[i])
            }

        }

    }

    print(images)
    print(resultImages)

}


func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    if(numberToolbar != nil)
    {
    numberToolbar!.removeFromSuperview()
    }

    if searchText.characters.count > 0 {
        self.searchBarActive    = true
        self.filterContentForSearchText(searchText)
        self.getSearchedFotos()
        self.collectionView?.reloadData()
    }else{

        self.searchBarActive = false
        self.collectionView?.reloadData()
    }

}


func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    if(numberToolbar != nil)
    {
        numberToolbar!.removeFromSuperview()
    }

    self.cancelSearching()



}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    if(numberToolbar != nil)
    {
        numberToolbar!.removeFromSuperview()
    }

     resultImages.removeAll()

        self.searchBar!.hidden = true




    self.searchBarActive = false

    self.collectionView?.reloadData()
    self.searchBar!.performSelector("resignFirstResponder", withObject: nil, afterDelay: 0.1)


    keyboardIsReal = false




}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {




}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    self.searchBarActive = false
    self.searchBar!.text = ""

    self.searchBar!.setShowsCancelButton(false, animated: false)

}

func cancelSearching(){
    self.searchBarActive = false
    self.searchBar!.text = ""
    resultImages.removeAll()

    self.collectionView?.reloadData()

}

最后一种方法是搜索特定项目。

0 个答案:

没有答案