Latest 0.0.2
Homepage https://github.com/gavrix/ImageViewModel
License MIT
Platforms ios 8.0
Dependencies Result, ReactiveCocoa
Frameworks Foundation
Authors

Carthage compatible
Cocoapods

RAC-based µ-ViewModel for managing images represented as URLs. Simplifies routine tasks of laoding images from the nethwork, caching, resizing, post-processing. Written purely in swift.

Usage

ImageViewModel class abstracts away all the logic around preparing the UIImage instance, uses dependency conforming to ImageProvider protocol for actual image loading and processing.

  1. Create suitable imageProvider by implementing ImageProvider protocol:

    struct SimpleImageProvider: ImageProvider {
    func image(image: UIImage, size: CGSize) -> SignalProducer<UIImage, NoError> {
        return resizeImage(size, image: image)
    }
    
    func image(url url: NSURL, size: CGSize) -> SignalProducer<UIImage, NoError> {
        return self.cachedDownloadSignal(url).flatMap(.Merge) { image in
            return self.cachedResizedImage(image, size:size, key: "(url)_(size)")
        }
    }
    
    ... // implement `cachedDownloadSignal` `cachedResizedImage` `resizeImage`
    }
  2. Create ImageViewModel instance with given ImageProvider and optionally default image, used while actual image is being prepared.

    self.imageViewModel = ImageViewModel(imageProvider: globalImageProvider)
  3. Connect ImageViewModel instance to UI:

    • set the ImageViewModel’s imageViewSize property, most of the time inside layoutSubviews of the UIView or in viewDidLayoutSubviews of the UIViewController:

      override func layoutSubviews() {
      super.layoutSubviews()
      self.imageViewModel.imageViewSize.value = self.userpicImageView.frame.size
      }
    • observe resultImage property from the ImageViewModel:

      self.imageViewModel.resultImage.producer.startWithNext {
          [unowned self] image in
          self.userpicImageView.image = image
      }
    • optionally, observe imageTransitionSignal signal from the ImageViewModel that triggers when image was delivered asynchronously:
      self.viewModel.userpicViewModel.imageTransitionSignal.observeNext {
          [unowned self] in
          let transition = CATransition()
          transition.type = kCATransitionFade
          self.userpicImageView?.layer.addAnimation(transition, forKey: nil)
      }
  4. Set ImageViewModel‘s image property to image URL (or local image as UIImage instance):
    self.imageViewModel.image.value = .URL(modelObject.imageURL)

That’s it. Everytime UIImageView size changes or new value is set to image property, ImageViewModel will attempt to prepare image through ImageProvider used and if image is being delivered asynchronously, it will propagate default image first, then prepared image as well as trigger imageTransitionSignal accordingly.

Installation

Direct checkout

Checkout this repository, copy ImageViewModel folder into your project’s 3rd party dependencies folder. Then drag ImageViewModel.xcodeproj into your master project. Don’t forget to add ImageViewModel in in your master project’s Target dependencies build phase.

Carthage

In your Cartfile add the following line:

git "https://github.com/gavrix/ImageViewModel.git" "0.0.1"

Example project.

Refer to example project in a collection of samples for other ViewModel based µ-frameworks here.

Credits

ImageViewModel created by Sergey Gavrilyuk @octogavrix.

License

ImageViewModel is distributed under MIT license. See LICENSE for more info.

Latest podspec

{
    "name": "ImageViewModel",
    "version": "0.0.2",
    "summary": "RAC-based lightweight ViewModel to handle images via given URL.",
    "description": "RAC-based u00b5-ViewModel for managing images represented as URLs. Simplifies routine tasks of laoding images from the nethwork, caching, resizing, post-processing. Written purely in swift.nswift 2.2 compatible",
    "homepage": "https://github.com/gavrix/ImageViewModel",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Sergey Gavrilyuk": "[email protected]"
    },
    "social_media_url": "http://twitter.com/octogavrix",
    "platforms": {
        "ios": "8.0"
    },
    "frameworks": "Foundation",
    "source": {
        "git": "https://github.com/gavrix/ImageViewModel.git",
        "tag": "0.0.2"
    },
    "source_files": "ImageViewModel/**/*.swift",
    "dependencies": {
        "Result": [
            "~> 2.0"
        ],
        "ReactiveCocoa": [
            "~> 4.1"
        ]
    }
}

Pin It on Pinterest

Share This