Paul Heinemeyer Paul Heinemeyer - 3 months ago 4
Swift Question

Images arent shown in my collectionviewcells. iOS(swift)

I got an array with all the images i got from the camera. They are saved in my coreData. If a user takes a new photo, the photo will be saved in the core data and then i replace the normal array data with the coreData images. So if i go back to the collection view, only the first and the last image is shown. When i print the images[indexpath.item] out i got the images. But they arent shown in the cells. I have a custom class for the imageView in the cell and the imageView has constraints, so the image cant be out of the visible area i think.
I look forward to solve this problem with your help. Thank you.

Here is the code from camera catch :

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);
}


And here is the code were i create the cell :

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]
let imageT : UIImage = images[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


}

Answer

Could you try and change: cell.imageView.frame = cell.frame to cell.imageView.frame.size = cell.frame.size

Comments