I have 2 functions saveImage and loadImage:
//saveImage
func saveImage(imageName: String, image: UIImage) {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileName = imageName
let fileURL = documentsDirectory.appendingPathComponent(fileName)
guard let data = image.jpegData(compressionQuality: 1) else { return }
if FileManager.default.fileExists(atPath: fileURL.path) {
do {
try FileManager.default.removeItem(atPath: fileURL.path)
print("Removed old image")
} catch let removeError {
print("couldn't remove file at path", removeError)
}
}
do {
try data.write(to: fileURL)
} catch let error {
print("error saving file with error", error)
}
}
//loadImage
func loadImageFromDocuments(fileName: String) -> UIImage? {
let documentDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)
if let dirPath = paths.first {
let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
let image = UIImage(contentsOfFile: imageUrl.path)
return image
}
return nil
}
}
When I call in tableviewcelll like this:
self.cachedImageView.saveImage(imageName:,image:)
self.cachedImageView.loadImageFromDocuments(fileName:)
I don't how know use that.
Create image loader class like below :-
class PKImageLoader {
let imageCache = NSCache<NSString, UIImage>()
class var sharedLoader: PKImageLoader {
struct Static {
static let instance: PKImageLoader = PKImageLoader()
}
return Static.instance
}
func imageForUrl(urlPath: String, completionHandler: #escaping (_ image: UIImage?, _ url: String) -> ()) {
guard let url = urlPath.toUrl else {
return
}
if let image = imageCache.object(forKey: urlPath as NSString) {
completionHandler(image, urlPath)
}
else {
URLSession.shared.dataTask(with: url) { data, _, _ in
guard let finalData = data else { return }
DispatchQueue.main.async {
if let img = UIImage(data: finalData) {
self.imageCache.setObject(img, forKey: urlPath as NSString)
completionHandler(img, urlPath)
}
}
}.resume()
}
}
}
download an image from URL and save it (by creating UIImage Array)
you can use that array as you want.
use below extension for setting image directly to image view.
extension UIImageView {
func setImage(from urlPath: String, placeHolder: UIImage? = nil) {
self.image = placeHolder
PKImageLoader.sharedLoader.imageForUrl(urlPath: urlPath) { image, _ in
self.image = image
}
}
this one also help you
extension String {
var toUrl: URL? {
if self.hasPrefix("https://") || self.hasPrefix("http://") {
return URL(string: self)
}
else {
return URL(fileURLWithPath: self)
}
}
Related
I have a problem with loading images from firebase. I have two functions. One of them collect info about user, second one load users avatar image. Unfortunately images load after function creates new user. I know it will be problem with asynchronous of Firebase but I don't know how to set up DispatchQueue to work properly. Can you help me with that?
// function that load user image in user manager class
func loadUserImage(contactUserID: String, completion: #escaping (UIImage) -> Void) {
let userID = Auth.auth().currentUser!.uid
var userImageRef = self.storage.child("\(userID)/userImage.jpg")
var image = UIImage()
if contactUserID != "" {
userImageRef = self.storage.child("\(contactUserID)/userImage.jpg")
}
userImageRef.getData(maxSize: 5 * 1024 * 1024) { (data, error) in
if let error = error {
print("Error with retrieving data: \(error.localizedDescription)")
} else {
if data?.count != 0 {
image = UIImage(data: data!)!
} else {
image = UIImage(systemName: "person.circle.fill")!
}
completion(image)
}
}
}
// function that load user in contact manager class
func loadContactList(completion: #escaping ([User]) -> Void) {
let currentUserID = Auth.auth().currentUser!.uid
db.collection("contacts")
.document(currentUserID)
.collection("userContacts")
.addSnapshotListener { (querySnapshot, error) in
var contactList = [User]()
if let error = error {
print("Error with retrieving data from DB: \(error.localizedDescription)")
} else {
if let snapshotDocuments = querySnapshot?.documents {
for document in snapshotDocuments {
let data = document.data()
let uid = data["uid"] as! String
let name = data["name"] as! String
let email = data["email"] as! String
var contact = User(email: email, name: name, userID: uid)
DispatchQueue.global().sync {
self.userService.loadUserImage(contactUserID: uid) { (image) in
contact.photoURL = image
}
}
contactList.append(contact)
contactList.sort {
$0.name < $1.name
}
completion(contactList)
}
}
}
}
}
// Function implementation in viewController
func loadContactList() {
self.contactService.loadContactList { (contactArray) in
self.contactList = contactArray
self.tableView.reloadData()
}
}
What you can do is to store the image url in the firebase database and after that create this extension:
import UIKit
let imageCache: NSCache = NSCache<AnyObject, AnyObject>()
extension UIImageView {
func loadImageUsingCacheWithUrlString(urlString: String) {
self.image = nil
if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = cachedImage
return
}
let url = URL(string: urlString)
if let data = try? Data(contentsOf: url!) {
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)
self.image = downloadedImage
}
})
}
}
}
And call:
if let url = data["imgUrl"] as? String {
self.myImageView.loadImageUsingCacheWithUrlString(urlString: url)
}
For that what you need to do is to create and initialize an UIImage object. If you are working with cell classes you need to create this object in the cell.
I have a UITextView and appending TextAttachments inside of it. I am getting photos from web url. Here is the code:
image.load(url: URL(string: ("http://www.furkan.webofisin.com/upload/media/5db1b96366cd8.jpg")))
imageAttachment.image = image.image
let image1String = NSAttributedString(attachment: imageAttachment)
fullString.append(image1String)
And this is the load function as extension:
var imageUrlString: String?
func load(urlString: String) {
imageUrlString = urlString
guard let url = URL(string: urlString) else {
DispatchQueue.main.async {
let urlMessage = "Hata"
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as NSString) {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url) { (data , response, error) in
if error != nil {
DispatchQueue.main.async {
let urlMessage = "Bir hata meydana geldi."
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache ?? UIImage(named: "astast")!, forKey: urlString as NSString)
}
}.resume()
}
Images not displaying at first time after refresh page its displaying because of caching.
I checked image.image as UIImage but its always nil before loading finished.
Here is the screenshot of first opening.
At first opening image data is nil
How can I detect and reload images when image data filled.
Add a completion as call to get image initially is asynchronous unlike from local cache
func load(urlString: String,completion:#escaping(() -> () )) {
imageUrlString = urlString
guard let url = URL(string: urlString) else {
DispatchQueue.main.async {
let urlMessage = "Hata"
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as NSString) {
self.image = imageFromCache
completion()
return
}
URLSession.shared.dataTask(with: url) { (data , response, error) in
if error != nil {
DispatchQueue.main.async {
let urlMessage = "Bir hata meydana geldi."
appDelegate.errorView(message: urlMessage, color: smoothGreenColor)
}
return
}
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache ?? UIImage(named: "astast")!, forKey: urlString as NSString)
completion()
}
}.resume()
}
Call
image.load(url: URL(string: ("http://www.furkan.webofisin.com/upload/media/5db1b96366cd8.jpg"))) {
imageAttachment.image = image.image
let image1String = NSAttributedString(attachment: imageAttachment)
fullString.append(image1String)
}
I make some code using swift 4 to load image from URL, but every time I add images to server, it took a lot of time to load it in colection view or table view. I want to try store it in NScache but i dont understand to do it. can anyone help me, I'm new in swift :(
#objc func loadPosts() {
let url = URL(string: "http://someURL/Url.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let body = "phomepost=\(homepost)"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
if error == nil {
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
self.comments.removeAll(keepingCapacity: false)
self.images.removeAll(keepingCapacity: false)
self.collectionView?.reloadData()
guard let parseJSON = json else {
print("Error While Parsing")
return
}
guard let posts = parseJSON["posts"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}
self.comments = posts.reversed()
for i in 0 ..< self.comments.count {
let path = self.comments[i]["path"] as? String
if !path!.isEmpty {
let url = NSURL(string: path!)!
let imageData = try? Data(contentsOf: url as URL)
let image = UIImage(data: imageData! as Data)!
self.images.append(image)
} else {
let image = UIImage()
self.images.append(image)
}
}
self.collectionView?.reloadData()
//print(posts.count)
} catch {
print(error)
}
}else{
print(error)
}
})
}.resume()
}
You can use something like this:
private let cache = NSCache<NSString, NSData>()
.....
func downloadImage(url: String, handler: #escaping(Data?, Error?) -> Void){
let cacheID = NSString(string: url)
if let cachedData = cache.object(forKey: cacheID) {
handler((cachedData as Data), nil)
}else{
if let url = URL(string: url) {
let session = URLSession(configuration: urlSessionConfig)
var request = URLRequest(url: url)
request.cachePolicy = .returnCacheDataElseLoad
request.httpMethod = "get"
session.dataTask(with: request) { (data, response, error) in
if let _data = data {
self.cache.setObject(_data as NSData, forKey: cacheID)
handler(_data, nil)
}else{
handler(nil, error)
}
}.resume()
} else {
// NetworkError is a custom error
handler(nil, NetworkError.invalidURL)
}
}
}
}
This will add a small animation while loading using image set.
let imageCache = NSCache<AnyObject, AnyObject>()
extension UIImageView {
func loadImageFromUrl(urlString: String) {
let loader1 = UIImage(named: "loaderImage1.png")
let loader2 = UIImage(named: "loaderImage2.png")
let loader3 = UIImage(named: "loaderImage3.png")
let imageArray = [loader1, loader2, loader3]
let animatedImage = UIImage.animatedImage(with: imageArray as! [UIImage], duration: 1.7)
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage{
self.image = imageFromCache
return
} else {
self.image = animatedImage
Alamofire.request(urlString, method: .get).response { (responseData) in
if let data = responseData.data {
DispatchQueue.main.async {
if let imageToCache = UIImage(data: data){
imageCache.setObject(imageToCache, forKey: urlString as AnyObject)
self.image = imageToCache
}
}
}
} //alamofire
}
}
}
I got a source code from a github page written in swift and implementing GoogleMaps. I now want to refactor the codes to use Alamofire and SwiftyJSON so that I can improve the code but I got confused because through my learning of swift I used Alamofire and swiftyJSON for every networking process so I am confused currently. the code below
typealias PlacesCompletion = ([GooglePlace]) -> Void
typealias PhotoCompletion = (UIImage?) -> Void
class GoogleDataProvider {
private var photoCache: [String: UIImage] = [:]
private var placesTask: URLSessionDataTask?
private var session: URLSession {
return URLSession.shared
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
func fetchPlacesNearCoordinate(_ coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: #escaping PlacesCompletion) -> Void {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true&key=\(appDelegate.APP_ID)"
let typesString = types.count > 0 ? types.joined(separator: "|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? urlString
guard let url = URL(string: urlString) else {
completion([])
return
}
if let task = placesTask, task.taskIdentifier > 0 && task.state == .running {
task.cancel()
}
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
placesTask = session.dataTask(with: url) { data, response, error in
var placesArray: [GooglePlace] = []
defer {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
completion(placesArray)
}
}
guard let data = data,
let json = try? JSON(data: data, options: .mutableContainers),
let results = json["results"].arrayObject as? [[String: Any]] else {
return
}
results.forEach {
let place = GooglePlace(dictionary: $0, acceptedTypes: types)
placesArray.append(place)
if let reference = place.photoReference {
self.fetchPhotoFromReference(reference) { image in
place.photo = image
}
}
}
}
placesTask?.resume()
}
func fetchPhotoFromReference(_ reference: String, completion: #escaping PhotoCompletion) -> Void {
if let photo = photoCache[reference] {
completion(photo)
} else {
let urlString = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=200&photoreference=\(reference)&key=\(appDelegate.APP_ID)"
guard let url = URL(string: urlString) else {
completion(nil)
return
}
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
session.downloadTask(with: url) { url, response, error in
var downloadedPhoto: UIImage? = nil
defer {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
completion(downloadedPhoto)
}
}
guard let url = url else {
return
}
guard let imageData = try? Data(contentsOf: url) else {
return
}
downloadedPhoto = UIImage(data: imageData)
self.photoCache[reference] = downloadedPhoto
}
.resume()
}
}
}
any help to refactor the codes to use Alamofire and swiftyJSON would be appreciated.
Both Alamofire and SwiftyJSON have pretty decent instructions, and there are plenty of examples online to look for. However, this would be a decent starting point - you need to replace your session.dataTask and session.downloadTask with Alamofire methods. For example, instead of:
session.downloadTask(with: url) { url, response, error in
var downloadedPhoto: UIImage? = nil
defer {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
completion(downloadedPhoto)
}
}
guard let url = url else {
return
}
guard let imageData = try? Data(contentsOf: url) else {
return
}
downloadedPhoto = UIImage(data: imageData)
self.photoCache[reference] = downloadedPhoto
}
.resume()
use this skeleton and implement your models and logic:
Alamofire
.request(url)
.responseJSON { dataResponse in
switch dataResponse.result {
case .success:
guard let json = JSON(dataResponse.data) else {
return
}
// Continue parsing
case .failure(let error):
// Handle error
print("\(error)")
}
}
I am working on show image from url async. I have tried to create a new thread for download image and then refresh on main thread.
func asyncLoadImg(product:Product,imageView:UIImageView){
let downloadQueue = dispatch_queue_create("com.myApp.processdownload", nil)
dispatch_async(downloadQueue){
let data = NSData(contentsOfURL: NSURL(string: product.productImage)!)
var image:UIImage?
if data != nil{
image = UIImage(data: data!)
}
dispatch_async(dispatch_get_main_queue()){
imageView.image = image
}
}
}
When I was trying to debug that, when it comes to dispatch_async(downloadQueue), it jumps out the func. Any suggestion? Thx
**Swift 5.0+ updated Code :
extension UIImageView {
func imageFromServerURL(_ URLString: String, placeHolder: UIImage?) {
self.image = nil
//If imageurl's imagename has space then this line going to work for this
let imageServerUrl = URLString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
if let url = URL(string: imageServerUrl) {
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
//print("RESPONSE FROM API: \(response)")
if error != nil {
print("ERROR LOADING IMAGES FROM URL: \(error)")
DispatchQueue.main.async {
self.image = placeHolder
}
return
}
DispatchQueue.main.async {
if let data = data {
if let downloadedImage = UIImage(data: data) {
self.image = downloadedImage
}
}
}
}).resume()
}
}
}
Now wherever you required just do this to load image from server url :
Using swift 5.0 + updated code using placeholder image :
UIImageView.imageFromServerURL(URLString:"here server url",placeHolder: placeholder image in uiimage format)
Simple !
Use extension in Swift3. To resolve Network problem i recommend you use NSCache:
import UIKit
let imageCache = NSCache<NSString, AnyObject>()
extension UIImageView {
func loadImageUsingCache(withUrl urlString : String) {
let url = URL(string: urlString)
self.image = nil
// check cached image
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}
// if not, download image from url
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
if let image = UIImage(data: data!) {
imageCache.setObject(image, forKey: urlString as NSString)
self.image = image
}
}
}).resume()
}
}
Hope it help!
Carrying on from Shobhakar Tiwari's answer, I think its often helpful in these cases to have a default image in case of error, and for loading purposes, so I've updated it to include an optional default image:
Swift 3
extension UIImageView {
public func imageFromServerURL(urlString: String, defaultImage : String?) {
if let di = defaultImage {
self.image = UIImage(named: di)
}
URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error ?? "error")
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
self.image = image
})
}).resume()
}
}
This solution make scrolling really fast without unnecessary image updates.
You have to add the url property to our cell class:
class OfferItemCell: UITableViewCell {
#IBOutlet weak var itemImageView: UIImageView!
#IBOutlet weak var titleLabel: UILabel!
var imageUrl: String?
}
And add extension:
import Foundation
import UIKit
let imageCache = NSCache<AnyObject, AnyObject>()
let imageDownloadUtil: ImageDownloadUtil = ImageDownloadUtil()
extension OfferItemCell {
func loadImageUsingCacheWithUrl(urlString: String ) {
self.itemImageView.image = nil
if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.itemImageView.image = cachedImage
return
}
DispatchQueue.global(qos: .background).async {
imageDownloadUtil.getImage(url: urlString, completion: {
image in
DispatchQueue.main.async {
if self.imageUrl == urlString{
imageCache.setObject(image, forKey: urlString as AnyObject)
self.itemImageView.image = image
}
}
})
}
}
}
You can also improve it and extract some code to a more general cell class i.e. CustomCellWithImage to make it more reusable.
Here this code might help you.
let cacheKey = indexPath.row
if(self.imageCache?.objectForKey(cacheKey) != nil){
cell.img.image = self.imageCache?.objectForKey(cacheKey) as? UIImage
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
if let url = NSURL(string: imgUrl) {
if let data = NSData(contentsOfURL: url) {
let image: UIImage = UIImage(data: data)!
self.imageCache?.setObject(image, forKey: cacheKey)
dispatch_async(dispatch_get_main_queue(), {
cell.img.image = image
})
}
}
})
}
With this image will download and cache without lagging the table view scroll
The most common way in SWIFT 4 to load async images without blink or changing images effect is use to custom UIImageView class like this one:
//MARK: - 'asyncImagesCashArray' is a global varible cashed UIImage
var asyncImagesCashArray = NSCache<NSString, UIImage>()
class AyncImageView: UIImageView {
//MARK: - Variables
private var currentURL: NSString?
//MARK: - Public Methods
func loadAsyncFrom(url: String, placeholder: UIImage?) {
let imageURL = url as NSString
if let cashedImage = asyncImagesCashArray.object(forKey: imageURL) {
image = cashedImage
return
}
image = placeholder
currentURL = imageURL
guard let requestURL = URL(string: url) else { image = placeholder; return }
URLSession.shared.dataTask(with: requestURL) { (data, response, error) in
DispatchQueue.main.async { [weak self] in
if error == nil {
if let imageData = data {
if self?.currentURL == imageURL {
if let imageToPresent = UIImage(data: imageData) {
asyncImagesCashArray.setObject(imageToPresent, forKey: imageURL)
self?.image = imageToPresent
} else {
self?.image = placeholder
}
}
} else {
self?.image = placeholder
}
} else {
self?.image = placeholder
}
}
}.resume()
}
}
example of use this class in UITableViewCell bellow:
class CatCell: UITableViewCell {
//MARK: - Outlets
#IBOutlet weak var catImageView: AyncImageView!
//MARK: - Variables
var urlString: String? {
didSet {
if let url = urlString {
catImageView.loadAsyncFrom(url: url, placeholder: nil)
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
One of the best way is to used SDWebImage.
Swift Example:
import SDWebImage
imageView.sd_setImage(with: URL(string: "ImageUrl"), placeholderImage: UIImage(named: "placeholder.png"))
Objective C Example:
#import <SDWebImage/UIImageView+WebCache.h>
[imageView sd_setImageWithURL:[NSURL URLWithString:#"ImageUrl"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];