【问题标题】:Images from array aren't shown in UICollectionviewUICollectionview 中不显示数组中的图像
【发布时间】:2016-12-12 04:42:20
【问题描述】:

我有一个数组,其中包含从相机获取的所有图像。它们保存在我的核心数据中。如果用户拍摄新照片,照片将保存在核心数据中,然后我将普通数组数据替换为核心数据图像。因此,如果我返回到集合视图,则只显示第一张和最后一张图像。当我打印出images[indexpath.item] 时,我得到了图像。但它们没有显示在单元格中。我在单元格中有一个imageView 的自定义类,imageView 有约束,所以我认为图像不能超出可见区域。我期待在您的帮助下解决这个问题。谢谢。

这是来自相机捕捉的代码:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {                    
    photoImage.image = image
    
    print(images)
    addPhotoLabel.removeFromSuperview()
    if isEdit == false {            
        let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let entinity = NSEntityDescription.entityForName("Image", inManagedObjectContext: context)
        
        let newImage = Image(entity: entinity!, insertIntoManagedObjectContext: context)
        newImage.image = UIImageJPEGRepresentation(image, 1)
        do {
            try context.save()
        } catch let error as NSError {
            print(error)
        }
        images.removeAll()
        let request = NSFetchRequest()
        request.entity = entinity
        let fetched = try? context.executeFetchRequest(request)
        if fetched != nil {
            let imagesS = fetched!.map { UIImage(data: ($0 as! Image).image!)}
            
            images.appendContentsOf(imagesS)
            
        }        
    }
    self.dismissViewControllerAnimated(true, completion: nil);
}

以及我创建单元格的代码:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1
   
    let cell : FlickrPhotoCell = 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 = images[indexPath.item]
        print(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)
    cell.placeHolderName.frame.size = CGSize(width: cell.frame.width, height: cell.frame.height/4)
 
    return cell
}

询问我是否需要更多代码。谢谢

【问题讨论】:

标签: ios swift image uicollectionview uicollectionviewcell


【解决方案1】:

这个答案可能会有所帮助:https://stackoverflow.com/a/14438875/2678994

此外,您可能希望(在您的 Storyboard 中或以编程方式)在 UICollectionViewCell 中设置一个 UIImageView,然后更新该图像视图的 .image 值。

【讨论】:

  • 非常感谢 :)
猜你喜欢
  • 2019-11-16
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多