AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

ASMultiplexImageNode not displaying image (even placeholder)

Open rajeevwhiz opened this issue 10 years ago • 6 comments

I have a ASCellNode which uses ASMultiplexImageNode.

here is the initializer code for ASMultiplexImageNode

public enum imageId: String {
   case original = "original"
   case medium = "medium"
   case thumb = "thumb"
}

class CustomImageCell: ASCellNode {
 var photo: Photo?
 var imageNode : ASMultiplexImageNode?
 required init(with photo : Photo) {
       super.init()
       self.photo = photo
       self.imageNode = ASMultiplexImageNode.init()
       self.imageNode?.downloadsIntermediateImages = true
       self.imageNode?.placeholderEnabled = true
       self.imageNode?.imageIdentifiers = [imageId.original as! NSCopying & NSObjectProtocol,
                                           imageId.medium as! NSCopying & NSObjectProtocol,
                                           imageId.thumb as! NSCopying & NSObjectProtocol]
       self.imageNode?.dataSource = self
       self.imageNode?.delegate = self

       self.addSubnode(self.imageNode!)
 }
 override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
       return ASInsetLayoutSpec.init(insets: UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0), child: imageNode!)
 }
}

Implemented the datasource and delegate:

extension CustomImageCell: ASMultiplexImageNodeDelegate, ASMultiplexImageNodeDataSource {
    func multiplexImageNode(_ imageNode: ASMultiplexImageNode, urlForImageIdentifier imageIdentifier: ASImageIdentifier) -> URL? {
        if imageIdentifier.isEqual(imageId.original) {
            return (photo?.fullImagePath == nil) ? nil : URL.init(string: (photo?.imagePath)!)
        } else if imageIdentifier.isEqual(imageId.thumb) {
            return (photo?.thumbnailPath == nil) ? nil : URL.init(string: (photo?.thumbnailPath)!)
        } else {
            return (photo?. mediumImagePath == nil) ? nil : URL.init(string: (photo?. mediumImagePath)!)
        }
    }
    
   override func placeholderImage() -> UIImage? {
        return UIImage(named: "placeholder")
   }
    
    func multiplexImageNode(_ imageNode: ASMultiplexImageNode, assetForLocalIdentifier assetLocalIdentifier: String) -> PHAsset? {
        return photo?.asset
    }
    
    func multiplexImageNode(_ imageNode: ASMultiplexImageNode, didUpdate image: UIImage?, withIdentifier imageIdentifier: ASImageIdentifier?, from previousImage: UIImage?, withIdentifier previousImageIdentifier: ASImageIdentifier?) {
        print("Image updated from \(previousImageIdentifier) to \(imageIdentifier)")
    }
}

The collection is displaying with empty cells. Even the placeholder image is not showing. What am I missing?

rajeevwhiz avatar Nov 08 '16 19:11 rajeevwhiz

For now I have bundled some images in the app and urlForImageIdentifier is returning following URL.. here is the debug output... URL=Optional(/var/containers/Bundle/Application/90D102CE-1F59-413C-AA08-715EF177109B/CustomExample.app/tn_pic1.jpg)

Does ASMultiplexImageNodeDelegate support local URLs? or is it expecting only HTTP / HTTPS? @appleguy @maicki

rajeevwhiz avatar Nov 08 '16 20:11 rajeevwhiz

@appleguy @maicki @Adlai-Holler
I looked at the Multiplex example in examples_extra (from master) and it doesn't seem to be working.

It's failing with

2016-11-08 14:40:57.753959 Sample[3896:1487694] *** Assertion failure in -[ASMultiplexImageNode initWithCache:downloader:], /Volumes/Home/rajeev/Apple/Facebook/AsyncDisplayKit/AsyncDisplayKit/ASMultiplexImageNode.mm:179 2016-11-08 14:40:57.758825 Sample[3896:1487694] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'downloader must respond to either downloadImageWithURL:callbackQueue:downloadProgress:completion:.'

rajeevwhiz avatar Nov 08 '16 22:11 rajeevwhiz

To isolate the issue, I updated urlForImageIdentifier and hardcoded URLs from the Multiplex example.

func multiplexImageNode(_ imageNode: ASMultiplexImageNode, urlForImageIdentifier imageIdentifier: ASImageIdentifier) -> URL? {
        if imageIdentifier.isEqual(imageId.thumb) {
            let url1 = URL(string: "https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples_extra/Multiplex/worst.png")
            print("URL=\(url1)")
            return url1
        }

        if imageIdentifier.isEqual(imageId.medium) {
            let url1 = URL(string: "https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples_extra/Multiplex/medium.png")
            print("URL=\(url1)")
            return url1
        }

        if imageIdentifier.isEqual(imageId.original) {
            let url1 = URL(string: "https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples_extra/Multiplex/best.png")
            print("URL=\(url1)")
            return url1
        }
        // unexpected identifier
        return nil;
    }

I can see this method is being called and correct URL is being returned. I also implemented ASImageDownloaderProtocol so I can see downloadProgress is being called but didFinishDownloadingImageWithIdentifier is never called. @appleguy Is that the reason why all my ImageCells are empty inside the ASCollectionNode?

rajeevwhiz avatar Nov 08 '16 22:11 rajeevwhiz

@appleguy some good news.. [NSURL URLWithAssetLocalIdentifier:targetSize:contentMode:options:] was the missing link to get the photos from photo lib working. I am able to load the image and imageNode is displaying it properly too !! It works perfect :)

However, loading image from the app with URL:

URL=file:///var/containers/Bundle/Application/F8478269-1902-4ECF-8B0D-42210B4B0FC4/WhizPics.app/tn_pic1.jpg

or from the network with URL:

URL=https://raw.githubusercontent.com/facebook/AsyncDisplayKit/master/examples_extra/Multiplex/worst.png

is not working. I am lost again!

rajeevwhiz avatar Nov 08 '16 23:11 rajeevwhiz

I spoke too soon... My ImageNode is width=121&height=112 and I want smallPhoto (imageId.thumb identifier) to be loaded there. However, urlForImageIdentifier method is being called for imageId.medium and imageId.original identifiers too. Check logs.. .

URLs are printed from urlForImageIdentifier method and

Image updated from ID1 to ID2

are printed from didUpdate method. I am not sure why images are being updated to higher resolution when the imageNode size is 121x112 which matches the thumbnail.

All this log is for single imageNode with single picture. It's called 15 times! Also the memory shot up from 20MB to 95MB just loading a single picture from the photo library :(

Thumb URL=ph://smallPhoto?width=121&height=112&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Thumb URL=ph://smallPhoto?width=121&height=112&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Thumb URL=ph://smallPhoto?width=121&height=112&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Thumb URL=ph://smallPhoto?width=121&height=112&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Medium URL=ph://mediumPhoto?width=1632&height=1224&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Image updated from nil to Optional(imageId.thumb)
Medium URL=ph://mediumPhoto?width=1632&height=1224&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Medium URL=ph://mediumPhoto?width=1632&height=1224&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Image updated from Optional(imageId.thumb) to Optional(imageId.thumb)
Medium URL=ph://mediumPhoto?width=1632&height=1224&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Image updated from Optional(imageId.thumb) to Optional(imageId.medium)
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Image updated from Optional(imageId.medium) to Optional(imageId.medium)
Original URL=ph://largePhoto?width=3264&height=2448&version=0&contentmode=1&network=0&resizemode=0&deliverymode=0
Image updated from Optional(imageId.medium) to Optional(imageId.original)
Image updated from Optional(imageId.original) to Optional(imageId.original)
Image updated from Optional(imageId.original) to Optional(imageId.original)

rajeevwhiz avatar Nov 09 '16 00:11 rajeevwhiz

Hi @rajeevwhiz I'm going to close this issue for now as it looks like the info is better in the other issue you opened.

garrettmoon avatar Nov 14 '16 21:11 garrettmoon