Backport of SwiftUI.AsyncImage to iOS 14, macOS 11, tvOS 14 and watchOS 7 and earlier.

Related tags

Image swift swiftui
Overview

SBPAsyncImage

Swift 5.4 SwiftUI Swift Package Manager Lincense

Backport of SwiftUI.AsyncImage to iOS 14, macOS 11, tvOS 14 and watchOS 7 and earlier.

AsyncImage is a view that asynchronously loads and displays an image.
However, AsyncImage is available from iOS 15, macOS 12, tvOS 15, and watchOS 8.
SBPAsyncImage provides interface and behavior of AsyncImage to earlier OS.

A Work In Progress

SBPAsyncImage is still in active development.
Please file all bugs, issues, and suggestions as an Issue in the GitHub repository.

Installation

Swift Package Manager

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SBPAsyncImageExample",
    platforms: [
        .iOS(.v13),
        .macOS(.v10_15),
        .tvOS(.v13),
        .watchOS(.v6),
    ],
    dependencies: [
        .package(url: "https://github.com/yutailang0119/SBPAsyncImage", .exact("0.1.0")),
    ],
    targets: [
        .target(
            name: "SBPAsyncImageExample",
            dependencies: ["SBPAsyncImage"]),
    ]
)

Usage

import SwiftUI
import SBPAsyncImage

struct ContentView: View {
    var body: some View {
        BackportAsyncImage(url: URL(string: "https://example.com/icon.png"))
            .frame(width: 200, height: 200)
    }
}

Custom placeholder

import SwiftUI
import SBPAsyncImage

struct ContentView: View {
    var body: some View {
        BackportAsyncImage(url: URL(string: "https://example.com/icon.png")) { image in
            image.resizable()
        } placeholder: {
            ProgressView()
        }
        .frame(width: 50, height: 50)
    }
}

Author

Yutaro Muta

License

The project is available under MIT Licence

Comments
  • If URL changes the new URL is not downloaded

    If URL changes the new URL is not downloaded

    Hey guys,

    I'm pretty new to SwiftUI therefore I'm not a big help fixing this, but I noticed that when changing the URL of the AsyncImage the new URL is not downloaded an displayed. I'm guessing the view needs to be destroyed first?

    Here is what I'm using and what does not work:

    ...
    if(libraryArtwork != nil) {
                AsyncImage(url: libraryArtwork!.artworkUrl(width: urlResolution(), height: urlResolution())!, content: { image in
                    image.resizable()
                }, placeholder: {
                    Rectangle().frame(width: width, height: width).foregroundColor(.gray)
                }).frame(width: width, height: width)
            } else {
                Rectangle().frame(width: width, height: width).foregroundColor(.gray)
            }
    ...
    

    So in this case the libraryArtwork might be changed externally because it is coming from the viewModel.

    Is the intended behaviour. Can I fix this somehow?

    opened by toadle 4
  • Update .gitignore for Swift 5.6.1

    Update .gitignore for Swift 5.6.1

    $swift --version
    swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
    Target: arm64-apple-macosx12.0
    
    $xcodebuild -version 
    Xcode 13.4
    Build version 13F17a
    

    Refs

    * https://github.com/apple/swift-package-manager/pull/2674
    * https://github.com/apple/swift-package-manager/pull/3511
    * https://github.com/apple/swift-package-manager/pull/3468
    
    opened by yutailang0119 0
  • Fix @\StateObject work as intended

    Fix @\StateObject work as intended

    Runtime error

    Accessing StateObject's object without being installed on a View. This will create a new instance each time.

    Solution

    • https://forums.swift.org/t/explanation-behind-the-error-accessing-stateobjects-object-without-being-installed-on-a-view-this-will-create-a-new-instance-each-time/40111
    opened by yutailang0119 0
  • BackportAsyncImage behave like @StateObject

    BackportAsyncImage behave like @StateObject

    With this implementation, ContentView body will be redrawn each time a button is pressed. Then, @ObserbedObject is also regenerated, and AsyncImagePhase revert to empty.

    struct ContentView: View {
        @State var count: Int = 0
    
        var body: some View {
            VStack {
                AsyncImage(url: URL(string: "https://example.com/icon.png"))
                Button {
                    count += 1
                } label: {
                    Text("Count: \(count)")
                }
            }
        }
    }
    

    This issue can be avoided by changing @ObserbedObject to @StateObject. However, @StateObject is not available on iOS 13/macOS 10.15/tvOS 13/watchOS 6, so resolved it using @State.

    References

    • https://kouki.hatenadiary.com/entry/2021/06/22/130000
    • https://github.com/ra1028/SwiftUI-Hooks/blob/main/Sources/Hooks/HookScope.swift
    opened by yutailang0119 0
  • `download()` called too soon

    `download()` called too soon

    Hello.

    I'm using your package in a project and I noticed that Xcode gives the warning:

    "Accessing StateObject's object without being installed on a View. This will create a new instance each time."

    at the point where self.viewModel.download() is called, inside a view initialiser.

    bug

    That's happening because the download is being initiated when the view is initialised rather than when it's added to the view hierarchy. I think you should consider fixing your implementation by moving all the self.viewModel.download() calls out of the initialisers they're in and into a closure passed to an onAppear() view modifier, like so:

    init(viewModel: ViewModel,
         @ViewBuilder content: @escaping (AsyncImagePhase) -> Content) {
        self._viewModel = .init(wrappedValue: viewModel)
        self.content = content
        // self.viewModel.download() // <--- remove this line
    }
    
    var body: some View {
        content(viewModel.phase)
            .onAppear { viewModel.download() } // <--- add this line
    }
    

    In any case, thanks for writing this package.

    opened by wtruppel 1
  • Support Scale

    Support Scale

    https://developer.apple.com/documentation/swiftui/asyncimage/init(url:scale:)

    scale The scale to use for the image. The default is 1. Set a different value when loading images designed for higher resolution displays. For example, set a value of 2 for an image that you would name with the @\2x suffix if stored in a file on disk.

    • [x] iOS
    • [ ] macOS
    • [x] tvOS
    • [x] watchOS
    help wanted 
    opened by yutailang0119 0
Releases(0.2.0)
  • 0.2.0(Oct 11, 2021)

    Compatibility

    • Following Xcode Version 13.0 (13A233)
    • ⚠️ Not support scale on macOS

    What's Changed

    • Download if URL change by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/17
    • Cleanup Package.swift by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/18

    Full Changelog: https://github.com/yutailang0119/SBPAsyncImage/compare/0.1.2...0.2.0

    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Jul 14, 2021)

    Compatibility

    • Following Xcode Version 13.0 beta (13A5155e) (a.k.a. Xcode 13 beta 2)
    • ⚠️ Not support scale on macOS

    What's Changed

    • Cleanup BackportAsyncImage by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/13
    • Fix @\StateObject work as intended by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/14
    • Rename BackportAsyncImage.ViewModel to Provider by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/15

    Full Changelog: https://github.com/yutailang0119/SBPAsyncImage/compare/0.1.1...0.1.2

    Source code(tar.gz)
    Source code(zip)
  • 0.1.1(Jul 6, 2021)

    Compatibility

    • Following Xcode Version 13.0 beta (13A5155e) (a.k.a. Xcode 13 beta 2)
    • ⚠️ Not support scale on macOS

    What's Changed

    • Remove duplicate unwanted image download by @yutailang0119 in https://github.com/yutailang0119/SBPAsyncImage/pull/12

    Full Changelog: https://github.com/yutailang0119/SBPAsyncImage/compare/0.1.0...0.1.1

    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Jul 6, 2021)

Owner
Yutaro Muta
Yutaro Muta
AsyncImageExample An example project for AsyncImage. Loading images in SwiftUI article.

AsyncImageExample An example project for AsyncImage. Loading images in SwiftUI article. Note: The project works in Xcode 13.0 beta (13A5154h).

Artem Novichkov 4 Dec 31, 2021
CachedAsyncImage is the simplest way to add cache to your AsyncImage.

SwiftUI CachedAsyncImage ??️ CachedAsyncImage is AsyncImage, but with cache capabilities. Usage CachedAsyncImage has the exact same API and behavior a

Lorenzo Fiamingo 278 Jan 5, 2023
A better way to operate QR Code in Swift, support iOS, macOS, watchOS and tvOS.

EFQRCode is a lightweight, pure-Swift library for generating stylized QRCode images with watermark or icon, and for recognizing QRCode from images, in

EFPrefix 4.3k Jan 2, 2023
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

Evgenii Neumerzhitckii 330 Sep 9, 2022
An iOS/tvOS photo gallery viewer, useful for viewing a large (or small!) number of photos.

This project is unmaintained. Alex passed away in an accident in late 2019. His love of iOS development will always be remembered. AXPhotoViewer AXPho

Alex Hill 596 Dec 30, 2022
SwiftUI App Icon Generator App for iOS & macOS Catalyst

SwiftUI App Icon Generator App for iOS & macOS Catalyst Generate Asset Icons easily to your iPhone, iPad, Mac, and Apple Watch Features The app has se

Alfian Losari 21 Sep 14, 2022
APNGKit is a high performance framework for loading and displaying APNG images in iOS and macOS.

APNGKit is a high performance framework for loading and displaying APNG images in iOS and macOS. It's built on top of a modified version of libpng wit

Wei Wang 2.1k Dec 30, 2022
The first non-jailbroken iOS (and macOS) application to adjust the screen temperature, brightness, and color!

GoodNight Project name thanks to @Emu4iOS. Based off of Thomas Finch's GammaThingy. GoodNight is an app that allows you to directly access the screen'

Anthony Agatiello 558 Nov 3, 2022
A ninepatch image render framework for iOS and MacOS

NinePatchKit NinePatch image parser and render framework for iOS & macOS Multilingual translation Chinese README Main Features parse png's binary data

Theo 14 Sep 30, 2022
A simple mesh viewer for MacOS based on Swift and Metal and using Assimp for loading meshes

Metal Mesh Viewer A simple triangle mesh viewer for MacOS This application is a simple (triangle) mesh viewer that should be capable of rendering even

J. Andreas Bærentzen 0 Dec 13, 2021
Screen translator for macOS with Apple Vision API and IBM Watson, Google Cloud Translator

Swifty-OCR-Translator Screen translator for macOS with Apple Vision API and IBM Watson, Google Cloud Translator Usage Select Translator Fill in the AP

Kwangmin Bae 21 Sep 13, 2022
Easily display images, animations, badges and alerts to your macOS application's dock icon

DSFDockTile Easily display images, animations, badges and alerts to your macOS application's dock icon. Why? I was inspired by Neil Sardesai after he

Darren Ford 45 Dec 2, 2022
A simple macOS app to read code from images, written purely in Swift using Vision Framework.

CodeReader A simple macOS app to read code from images, written purely in Swift using Vision Framework. Usage Drag an image Click the convert button R

Md Ibrahim Hassan 44 Nov 20, 2022
A python library to run metal compute kernels on MacOS 12

metalcompute for Python A python library to run metal compute kernels on MacOS Usage Example execution from M1-based Mac running MacOS 12.0: > ./build

Andrew Baldwin 21 Nov 7, 2022
Linearmouse - For macOS mouse users

LinearMouse Reverse scrolling, Linear scrolling, Universal back & forward Cursor

LinearMouse 1.6k Jan 3, 2023
Ioreg2plist - Dumps macOS IORegistry entries in plist format

ioreg2plist A command line tool to dump the macOS IORegistry entries in plist fo

null 2 Feb 15, 2022
Patch out the GPU checks for any x86-64 macOS Unreal Engine-based game

UnrealGPUPatcher Download here Patch out the GPU checks for any x86-64 macOS Unreal Engine-based game, particularly ARK: Survival Evolved. Requirement

Jacob Greenfield 35 Jan 1, 2023
A Swift/SwiftUI utility for caching and displaying images in SwiftUI Views

A Swift/SwiftUI utility for caching and displaying images asynchronously. Built with Swift 5.5 and works with async/await.

王雪铮 Xuezheng Wang 1 May 5, 2022
A demo of face recognition SwiftUI app on iOS. Based on Vision, OpenCV, Dlib and ResNet.

iOS-FaceRecognizer A demo of face recognition SwiftUI app on iOS, build for iPad. Based on Vision, OpenCV, Dlib and ResNet. Features Add face image an

js_john 11 Aug 20, 2022