UICollectionViewCell Remote Image Disappears on Scroll - ios

I have a horizontal scroll UICollectionView in a view controller. Whenever the view is scrolled, the images in the view disappear. I've tried to prepare for reuse with no success. I've posted the code below:
Collection View Cell
import UIKit
import SDWebImage
class StickerCell: UICollectionViewCell {
#IBOutlet weak var stickerImage: UIImageView!
override func prepareForReuse() {
super.prepareForReuse()
self.stickerImage.image = nil
}
func setImage(image: String) {
let url = NSURL(string: image)
self.stickerImage.contentMode = .ScaleAspectFit
self.stickerImage.clipsToBounds = true
self.stickerImage.center = self.center
self.stickerImage.sd_setImageWithURL(url)
}
}
View Controller
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.stickers.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell
let sticker = stickers[indexPath.row]
if let image = sticker["images"]["medium"].string {
cell.setImage(image)
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath)
if cell?.selected == true {
let sticker = stickers[indexPath.row]
if let image = sticker["images"]["medium"].string {
let url = NSURL(string: image)
self.stickerView.contentMode = .ScaleAspectFit
self.stickerView.clipsToBounds = true
self.stickerView.sd_setImageWithURL(url)
}
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(115, 115)
}

Try this..
-> Remove setImage method from strickerCell class.
-> Implement this
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell
let sticker = stickers[indexPath.row]
if let image = sticker["images"]["medium"].string {
let url = NSURL(string: image)
cell.stickerImage.contentMode = .ScaleAspectFit
cell.stickerImage.clipsToBounds = true
cell.stickerImage.sd_setImageWithURL(url, placeholderImage:placeHolderImage, completed: { (image, error, cacheType, url) -> Void in
cell.stickerImage.image = image
})
}
return cell
}

I tied this the same way but the images seem to disappear over a period or time. Try setting some options on the Sd_setImage Call: for example:
self.userProfile.sd_setImage(with: URL(string: self.senderData.userProfileimg), placeholderImage: #imageLiteral(resourceName: "blankProfile"), options: [SDWebImageOptions.progressiveDownload, SDWebImageOptions.continueInBackground], completed: { (image, error, CachedType, url) in
self.userProfile.image = image
})

Related

Swift: ScrollView Scrolling as per Cell Image Size

I have a viewController which has CollectionView and the collectionView cell contain the ImageView and response is from the api. And the viewController orientation is Landscape. So issue is when i scroll the image it's not scrolling properly. Here is the attach image.
So how can I set the Scrolling action as per the my image Size. So it's display only image in View. Here is my code.
extension GalleryViewController : UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
let image = arrImgs[indexPath.row]
cell.lblIndex.text = "< \(indexPath.row+1) / \(arrImgs.count) > Images"
if let imgUrl = URL.init(string: image.Message) {
cell.imgV.sd_setImage(with: imgUrl) { (img, error, casheType, url) in
DispatchQueue.main.async {
cell.imgV.image = img
}
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrImgs.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(width: width, height: height)
}
}
class ImageCell: UICollectionViewCell {
#IBOutlet var lblIndex: UILabel!
#IBOutlet var imgV: UIImageView!
}
This is my StoryBord Arrangement.
Thanks.

UICollectionView with array of images set it right

I have a question. Inside UICollectionView I have UIImage with Content Mode - Aspect Fit... Now I need that in UICollectionView images show one near another. Now I have a lot of space between them.
extension UserDetailInformationOfAnimalViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
//MARK: - Collections
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let post = postsArray[collectionView.tag]
// print("count: ", post.imagesLink.count)
return post.imageLinks.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
let post = postsArray[collectionView.tag]
// print("postArrayPhotos: ", post.imagesLink.count)
if post.imageLinks.count != 0 {
// print("POST = ", post)
// print("Look here: ", post.imagesLink[indexPath.row].imageLink)
// print("IMAGELINK = ", post.imagesLink[indexPath.row].imageLink)
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
print("IMAGELINK = ", imageLink)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = self.view.frame.size.height;
let width = self.view.frame.size.width;
// in case you you want the cell to be 40% of your controllers view
return CGSize(width: width, height: height * 0.35)
}
}
Use this method to set width of cell dynamically
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if indexPath.row == whatever you want {
return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.width/2)
}
}
I understand your problem. Here I give you a way around. This solution may not effective for you but it will give you idea how to solve it. Main concept is the you have to return cell size according to image. But instantly image is not in your hand. Its on the server. So you can do that fist return a demo or place holder image height then after finishing download actual image from server tell the collection view to re-layout the cell according to actual image size.
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
//told the collection view to relayout according to image size
collectionView.collectionViewLayout.invalidateLayout()
})
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
return cell.imageOfAnimalInCollectionView.image.size! // Put your logic here, until actual image download from server you can return a default size.
}

CollectionView in TableViewCell scroll isn't smooth in swift3

In my project CollectionView in TableViewCell isn't display and I added
cell.collectionView.reloadData()
in my code. After I added, CollectionView displayed in TableViewCell, but
ScrollView isn't smooth. How to solve this problem. If someone have any experience or ideas help me. Thanks.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell
let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row]
DispatchQueue.main.async {
cell.categoryTitle.text = mainThemeList.main_name
cell.collectionView.reloadData()
}
return cell
}
CollectionView in my project,
extension HomeCategoryRowCell : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
debugPrint("assetsTable count \(assetsTable.count)")
return assetsTable.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell
let list = assetsTable[(indexPath as NSIndexPath).row]
let url2 = NSURL(string:list.poster_image_url)
let data2 = NSData(contentsOf:url2! as URL)
// debugPrint(list.poster_image_url)
DispatchQueue.main.async(){
cell.movieTitle.text = list.name
cell.imageView.image = UIImage(data: data2! as Data)
}
return cell
}
}
Download Image Url Data in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath delegate - ASYNCHRONOUSLY
Create a Class
First cache the image Data with respect to key Image URL
Why do we need to cache the data?
Because - While scrolling we don't need to download Image Every Time so Cache the already downloaded Image Data and return the cache data
I created a class name: AsyncImageLoader then I created a function which is a completion handler which returns the data after completion of downloading Image
class AsyncImageLoader {
static let cache = NSCache<NSString, NSData>()
class func image(for url: URL, completionHandler: #escaping(_ image: UIImage?) -> ()) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
if let data = self.cache.object(forKey: url.absoluteString as NSString) {
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
return
}
guard let data = NSData(contentsOf: url) else {
DispatchQueue.main.async { completionHandler(nil) }
return
}
self.cache.setObject(data, forKey: url.absoluteString as NSString)
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
}
}
}
How to use AsyncImageLoader Class?
See below How I used in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let yourCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionCell", for: indexPath) as! CustomCollectionCell
yourCell.url = URL(string: "\(item_ImageUrl)")
return yourCell
}
CustomCollectionCell
class CustomCollectionCell: UICollectionViewCell {
#IBOutlet weak var cellImageDisplayView: UIImageView!
var url: URL? {
didSet {
if let url = url {
cellImageDisplayView.image = nil
AsyncImageLoader.image(for: url, completionHandler: { (image) in
DispatchQueue.main.async {
if let img = image {
self.cellImageDisplayView.image = img
}else{
let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage
}
}
})
}else{
let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage
}
}
}
}
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.main.scale
After I added above code , my project become more smooth. I'll try the best and back with the best solution. Thanks
This is because you are directly getting data from url and then image from data in main thread. You should use image downloading library to download the image from url
Here is the good third party to download image from url asynchronously.
https://github.com/rs/SDWebImage
Try with this I am 100% sure it will solve your problem.
Thanks

passing values from uitableview to uicollectionview

How can I pass parsed values from a url which has been used in an UITableview to a UICollectionview(scrolled horizontally) which has been used in each of the UITableviews cell? I want do this in swift language.
I am using a model with the name "collectionCat".
import Foundation
public class CollectionCat{
var brandid:String = ""
var brandname:String = ""
var result:String = ""
var noitems:String = ""
}
I am also using Alamofire to parse data from url.
override func viewDidLoad() {
super.viewDidLoad()
self.startActivityAnimating(message: "", type: NVActivityIndicatorType.BallClipRotate, color: AppColor.AppRed, padding: 10)
Alamofire.request(.POST, "http://goringr.co.in/Mathrubhumi_project_ios/brand.php", parameters: ["": ""])
.validate()
.responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")
self.jsonResultParse(response.result.value!)
self.stopActivityAnimating()
case .Failure(let error):
print(error)
self.stopActivityAnimating()
// return userCatagory
}
}
// Do any additional setup after loading the view.
}
if JSONArray.count != 0 {
//_ = [UserCatagory]()
for i:Int in 0 ..< JSONArray.count {
let jObject = JSONArray[i] as! NSDictionary
let uCollect:CollectionCat = CollectionCat()
uCollect.brandid = (jObject["brandid"] as AnyObject? as? String) ?? ""
uCollect.brandname = (jObject["brandname"] as AnyObject? as? String) ?? ""
uCollect.result = (jObject["result"] as AnyObject? as? String) ?? ""
uCollect.noitems = (jObject["noitems"] as AnyObject? as? String) ?? ""
collect.append(uCollect)
print("collect COUNT ",collect.count)
}
self.newTable.reloadData()
}
}
What i dont know is how to transfer the stored data to UICollection view which is in each cell of the mentioned UITableview.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// collectMaster = collect[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SecondHomeViewCell
// collectMaster = CollectionCat()
cell.get = ????
return cell
}
Can anyone Please help me with this?
The SecondHomeViewCell's code goes like this.
import UIKit
import Kingfisher
import Alamofire
import NVActivityIndicatorView
class SecondHomeViewCell: UITableViewCell{
#IBOutlet weak var collectionView: UICollectionView!
var genre:CollectionCat? = nil{
didSet {
collectionView.reloadData()
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//return genre!.brandid
// return genre
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("secondHome", forIndexPath: indexPath) as! SecondHomeCollectionViewCell
if let genre = genre {
cell.genre = genre.brandid[indexPath.row]
}
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemsPerRow:CGFloat = 4
let hardCodedPadding:CGFloat = 5
let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
return CGSize(width: itemWidth, height: itemHeight)
}
}
Add a array in your ViewController, call it
var collCat = [CollectionCat]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let collectMaster = collCat[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SecondHomeViewCell
cell.brandID = collectMaster.brandid
//and so on
return cell
}
If this is not the answer you are looking for, please tell me. Because I wanted to clarify but I don't have the ability to comment yet. I need to reach 50 reputation to be able to comment and ask some clarifications.
You can pass data to collection view by creating function in UITableviewCell custom class
class SecondHomeViewCell: UITableViewCell{
//Array containing modals for particular tableview cell index path
var arrDataFromViewController = NSArray()
func getData(arrData:NSArray){
arrDataFromViewController = arrData
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
#IBOutlet weak var collectionView: UICollectionView!
var genre:CollectionCat? = nil{
didSet {
collectionView.reloadData()
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//return genre!.brandid
// return genre
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("secondHome", forIndexPath: indexPath) as! SecondHomeCollectionViewCell
if let genre = genre {
cell.genre = genre.brandid[indexPath.row]
}
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemsPerRow:CGFloat = 4
let hardCodedPadding:CGFloat = 5
let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
return CGSize(width: itemWidth, height: itemHeight)
}
}
//Now in tableview data source method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// collectMaster = collect[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SecondHomeViewCell
// collectMaster = CollectionCat()
cell.getData = yourArray[indexPath.row]//Get Array data
return cell
}

Animating a CollectionViewCell Size To Outside CollectionView Boundary - Swift

I have been working on animating the size of some collectionViewCells. It is working great in my collectionViewController, but in another UIViewController where I have a small collectionView alongside other objects, the cell will only grow to the size of the collectionView. I would like it to fill the whole screen. What is the best way about doing this?
Here is my current code:
var largePhotoIndexPath : NSIndexPath? {
didSet{
var indexPaths = [NSIndexPath]()
if largePhotoIndexPath != nil {
indexPaths.append(largePhotoIndexPath!)
}
if oldValue != nil {
indexPaths.append(oldValue!)
}
collectionView?.performBatchUpdates({ () -> Void in
self.collectionView?.reloadItemsAtIndexPaths(indexPaths)
return
}){
completed in
if self.largePhotoIndexPath != nil {
self.collectionView?.scrollToItemAtIndexPath(self.largePhotoIndexPath!, atScrollPosition: .CenteredVertically, animated: true)
}
}
}
}
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
if largePhotoIndexPath == indexPath {
largePhotoIndexPath = nil
}
else {
largePhotoIndexPath = indexPath
}
return false
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let dealImage: UIImage = UIImage(data: self.imageDataArray[indexPath.row])!
let dealPhoto: UIImageView = UIImageView(image: dealImage)
if indexPath == largePhotoIndexPath {
var size = UIScreen.mainScreen().bounds
dealPhoto.frame == size
return CGSize(width: 300, height: 525)
}
return CGSize(width: 100, height: 175)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageDataArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! TrailInfoCollectionViewCell
cell.imageView.image = UIImage(data: self.imageDataArray[indexPath.row])
return cell
}
And some images before and after I select the cell:
As you can see, the image has grown to the size I want it, but it is constrained by the collectionView. Is it possible for a collectionViewCell to exceed the boundaries of a collectionView, or will I have to do it some other way? I would just set up a hidden imageView but I like the animated transition.
Thanks for your help. Please let me know if you need any more details.
clipsToBounds properties is what prevents a subview from growing past it's parent. Set it to false and it should work.

Resources