Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jkopelioff/94a2db6d107a9d759e10 to your computer and use it in GitHub Desktop.

Select an option

Save jkopelioff/94a2db6d107a9d759e10 to your computer and use it in GitHub Desktop.
UIImageView+AFNetworking+UIActivityIndicator
//
// UIImageView+AFNetworking+UIActivityIndicator.swift
//
// Created by Joel Kopelioff on 8/6/15.
// Copyright (c) 2015 DemandMedia Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import UIKit
import ObjectiveC
// Declare a global var to produce a unique address as the assoc object handle
var kUIActivityIndicatorView: UInt8 = 0
extension UIImageView
{
var activityIndicatorView:UIActivityIndicatorView? {
get {
return objc_getAssociatedObject(self, &kUIActivityIndicatorView) as? UIActivityIndicatorView
}
set {
objc_setAssociatedObject(self, &kUIActivityIndicatorView, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
func cancelImageRequestOperationAndRemoveActivityIndicatorView() {
if let activityIndicator = self.activityIndicatorView {
self.af_removeActivityIndicator()
}
}
func setImageWithURL(url:NSURL, usingActivityIndicatorStyle style:UIActivityIndicatorViewStyle)
{
self.setImageWithURLRequest(NSURLRequest(URL: url), placeholderImage: nil, usingActivityIndicatorStyle:style, success: nil, failure: nil)
}
func setImageWithURLRequest(urlRequest:NSURLRequest!, placeholderImage:UIImage!, usingActivityIndicatorStyle style:UIActivityIndicatorViewStyle, success: ((NSURLRequest!, NSHTTPURLResponse!, UIImage!) -> Void)!, failure: ((NSURLRequest!, NSHTTPURLResponse!,NSError!) -> Void)!) {
if urlRequest != nil {
self.af_addActivityIndicatorWithStyle(style)
}
self.setImageWithURLRequest(urlRequest, placeholderImage: placeholderImage,
success: {[weak self] (request:NSURLRequest!, response:NSHTTPURLResponse!, image:UIImage!) -> Void in
self?.af_removeActivityIndicator()
if success != nil {
success(request, response, image)
} else {
self?.image = image
}
}, failure: { [weak self] (request:NSURLRequest!, response:NSHTTPURLResponse!, error:NSError!) -> Void in
self?.af_removeActivityIndicator()
if failure != nil {
failure(request, response, error)
}
})
}
//MARK: - Private Helpers
func af_addActivityIndicatorWithStyle(style:UIActivityIndicatorViewStyle) {
if self.activityIndicatorView == nil {
self.activityIndicatorView = self.af_createActivityIndicatorWithStyle(style)
if NSThread.isMainThread() {
self.addSubview(self.activityIndicatorView!)
self.activityIndicatorView!.startAnimating()
} else {
dispatch_async(dispatch_get_main_queue()) {
self.addSubview(self.activityIndicatorView!)
self.activityIndicatorView!.startAnimating()
}
}
return
}
if NSThread.isMainThread() {
self.activityIndicatorView!.startAnimating()
} else {
dispatch_async(dispatch_get_main_queue()) {
self.activityIndicatorView!.startAnimating()
}
}
}
func af_removeActivityIndicator() {
if self.activityIndicatorView != nil {
self.activityIndicatorView?.removeFromSuperview()
self.activityIndicatorView = nil
}
}
func af_createActivityIndicatorWithStyle (style:UIActivityIndicatorViewStyle) -> UIActivityIndicatorView
{
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle:style)
activityIndicator.userInteractionEnabled = false
activityIndicator.center = self.center
activityIndicator.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
return activityIndicator
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment