Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature KF builder #1546

Merged
merged 8 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Demo/Demo/Kingfisher-Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17147" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="peg-r0-mlo">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="peg-r0-mlo">
<device id="retina5_5" orientation="portrait" appearance="dark"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17120"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -466,8 +466,12 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YVS-ul-f2n">
<rect key="frame" x="186" y="379.66666666666669" width="42" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YVS-ul-f2n">
<rect key="frame" x="82" y="365" width="250" height="50"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="250" id="hkg-9V-168"/>
<constraint firstAttribute="height" constant="50" id="oop-I0-zGm"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class AVAssetImageGeneratorViewController: UIViewController {
assetURL: URL(string: "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1280_10MG.mp4")!,
seconds: 15.0
)

imageView.kf.setImage(with: provider) { r in
print(r)
}
KF.dataProvider(provider).set(to: imageView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ class GIFViewController: UIViewController {
let url = ImageLoader.gifImageURLs.last!

// Should need to use different cache key to prevent data overwritten by each other.
KF.url(url, cacheKey: "\(url)-imageview").set(to: imageView)

let imageViewResource = ImageResource(downloadURL: url, cacheKey: "\(url)-imageview")
imageView.kf.setImage(with: imageViewResource)

let animatedImageViewResource = ImageResource(downloadURL: url, cacheKey: "\(url)-animated_imageview")
animatedImageView.kf.setImage(with: animatedImageViewResource)
KF.url(url, cacheKey: "\(url)-animated_imageview").set(to: animatedImageView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ class HighResolutionCollectionViewController: UICollectionViewController {
let url = ImageLoader.highResolutionImageURLs[indexPath.row % ImageLoader.highResolutionImageURLs.count]
// Use different cache key to prevent reuse the same image. It is just for
// this demo. Normally you can just use the URL to set image.
let resource = ImageResource(downloadURL: url, cacheKey: "\(url.absoluteString)-\(indexPath.row)")


// This should crash most devices due to memory pressure.
// let resource = ImageResource(downloadURL: url, cacheKey: "\(url.absoluteString)-\(indexPath.row)")
// imageView.kf.setImage(with: resource)

// This would survive on even the lowest spec devices!
imageView.kf.setImage(
with: resource,
options: [
.processor(DownsamplingImageProcessor(size: CGSize(width: 250, height: 250))),
.cacheOriginalImage
]
)
KF.url(url, cacheKey: "\(url.absoluteString)-\(indexPath.row)")
.downsampling(size: CGSize(width: 250, height: 250))
.cacheOriginalImage()
.set(to: imageView)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class ImageDataProviderCollectionViewController: UICollectionViewController {

let pair = model[indexPath.row]
let provider = UserNameLetterIconImageProvider(userNameFirstLetter: pair.0, backgroundColor: pair.1)
cell.cellImageView.kf.setImage(with: provider, options:[.processor(RoundCornerImageProcessor(cornerRadius: 75))])
KF.dataProvider(provider)
.roundCorner(radius: .point(75))
.set(to: cell.cellImageView)

return cell
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ class IndicatorCollectionViewController: UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell
cell.cellImageView.kf.indicatorType = selectedIndicatorType
cell.cellImageView.kf.setImage(
with: ImageLoader.sampleImageURLs[indexPath.row],
options: [.memoryCacheExpiration(.expired), .diskCacheExpiration(.expired)])
KF.url(ImageLoader.sampleImageURLs[indexPath.row])
.memoryCacheExpiration(.expired)
.diskCacheExpiration(.expired)
.set(to: cell.cellImageView)
return cell
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class InfinityCollectionViewController: UICollectionViewController {

// Mark each row as a new image.
let resource = ImageResource(downloadURL: url, cacheKey: "key-\(indexPath.row)")
cell.cellImageView.kf.setImage(with: resource)
KF.resource(resource).set(to: cell.cellImageView)

return cell
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class NormalLoadingViewController: UICollectionViewController {
super.viewDidLoad()
title = "Loading"
setupOperationNavigationBar()
collectionView?.prefetchDataSource = self
}
}

Expand All @@ -57,18 +56,14 @@ extension NormalLoadingViewController {
forItemAt indexPath: IndexPath)
{
let imageView = (cell as! ImageCollectionViewCell).cellImageView!
imageView.kf.setImage(
with: ImageLoader.sampleImageURLs[indexPath.row],
placeholder: nil,
options: [.transition(.fade(1)), .loadDiskFileSynchronously],
progressBlock: { receivedSize, totalSize in
print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { result in
print(result)
print("\(indexPath.row + 1): Finished")
}
)
let url = ImageLoader.sampleImageURLs[indexPath.row]
KF.url(url)
.fade(duration: 1)
.loadDiskFileSynchronously()
.progress { (received, total) in print("\(indexPath.row + 1): \(received)/\(total)") }
.done { print($0) }
.catch { err in print("Error: \(err)") }
.set(to: imageView)
}

override func collectionView(
Expand All @@ -82,10 +77,3 @@ extension NormalLoadingViewController {
return cell
}
}

extension NormalLoadingViewController: UICollectionViewDataSourcePrefetching {
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
let urls = indexPaths.compactMap { ImageLoader.sampleImageURLs[$0.row] }
ImagePrefetcher(urls: urls).start()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ class ProcessorCollectionViewController: UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell
let url = ImageLoader.sampleImageURLs[indexPath.row]
var options: KingfisherOptionsInfo = [.processor(currentProcessor)]
if currentProcessor is RoundCornerImageProcessor {
options.append(.cacheSerializer(FormatIndicatedCacheSerializer.png))
}
cell.cellImageView.kf.setImage(with: url, options: options) { result in
print(result)
}

KF.url(url)
.setProcessor(currentProcessor)
.serialize(as: .PNG)
.done { print($0) }
.catch { print($0) }
.set(to: cell.cellImageView)

return cell
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,24 @@ class ProgressiveJPEGViewController: UIViewController {
isFastestScan: isFastestScan,
scanInterval: 0.1
)

imageView.kf.setImage(
with: ImageLoader.progressiveImageURL,
placeholder: nil,
options: [.loadDiskFileSynchronously,
.progressiveJPEG(progressive),
.processor(processor)],
progressBlock: { receivedSize, totalSize in

KF.url(ImageLoader.progressiveImageURL)
.loadDiskFileSynchronously()
.progressiveJPEG(progressive)
.roundCorner(radius: .point(30))
.progress { receivedSize, totalSize in
print("\(receivedSize)/\(totalSize)")
self.progressLabel.text = "\(receivedSize) / \(totalSize)"
},
completionHandler: { result in
do {
let value = try result.get()
print(value)
print("Finished")

} catch {
self.progressLabel.text = error.localizedDescription
}
}
)
.done { result in
print(result)
print("Finished")
}
.catch { error in
print(error)
self.progressLabel.text = error.localizedDescription
}
.set(to: imageView)
}

override func alertPopup(_ sender: Any) -> UIAlertController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ class TextAttachmentViewController: UIViewController {
let attributedText = NSMutableAttributedString(string: "Hello World")

let textAttachment = NSTextAttachment()
textAttachment.kf.setImage(
with: URL(string: "https://onevcat.com/assets/images/avatar.jpg")!,
attributedView: label,
options: [
.processor(
ResizingImageProcessor(referenceSize: .init(width: 30, height: 30))
|> RoundCornerImageProcessor(cornerRadius: 15))
]
)
attributedText.replaceCharacters(in: NSRange(), with: NSAttributedString(attachment: textAttachment))
label.attributedText = attributedText

KF.url(URL(string: "https://onevcat.com/assets/images/avatar.jpg")!)
.resizing(referenceSize: CGSize(width: 30, height: 30))
.roundCorner(radius: .point(15))
.set(to: textAttachment, attributedView: label)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class TransitionViewController: UIViewController {

let t = makeTransition(type: transitionType, duration: duration)
let url = ImageLoader.sampleImageURLs[0]
imageView.kf.setImage(with: url, options: [.forceTransition, .transition(t)])
KF.url(url)
.forceTransition()
.transition(t)
.set(to: imageView)
}
}

Expand Down
12 changes: 5 additions & 7 deletions Demo/Demo/Kingfisher-macOS-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ extension ViewController: NSCollectionViewDataSource {
let url = URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.item + 1).jpg")!

item.imageView?.kf.indicatorType = .activity
item.imageView?.kf.setImage(with: url, placeholder: nil, options: nil,
progressBlock: { receivedSize, totalSize in
print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { result in
print("\(indexPath.item + 1): Finished")
})
KF.url(url)
.roundCorner(radius: .point(20))
.progress { receivedSize, totalSize in print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)") }
.done { print($0) }
.set(to: item.imageView!)

// Set imageView's `animates` to true if you are loading a GIF.
// item.imageView?.animates = true
Expand Down
4 changes: 4 additions & 0 deletions Kingfisher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
D16FEA5523079707006E67D5 /* NSButtonExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 185218B51CC07F8300BD58DE /* NSButtonExtensionTests.swift */; };
D1839845216E333E003927D3 /* Delegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1839844216E333E003927D3 /* Delegate.swift */; };
D186696D21834261002B502E /* ImageDrawingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D186696C21834261002B502E /* ImageDrawingTests.swift */; };
D18B3222251852E100662F63 /* KF.swift in Sources */ = {isa = PBXBuildFile; fileRef = D18B3221251852E100662F63 /* KF.swift */; };
D19ADD0C23099E3B00D20B28 /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1ED2D351AD2D09F00CFC3EB /* Kingfisher.framework */; };
D1A1CC9A219FAB4B00263AD8 /* Source.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A1CC99219FAB4B00263AD8 /* Source.swift */; };
D1A1CC9F21A0F98600263AD8 /* ImageDataProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A1CC9E21A0F98600263AD8 /* ImageDataProviderTests.swift */; };
Expand Down Expand Up @@ -248,6 +249,7 @@
D16FEA3923078C63006E67D5 /* LSStubResponseDSL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LSStubResponseDSL.h; sourceTree = "<group>"; };
D1839844216E333E003927D3 /* Delegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Delegate.swift; sourceTree = "<group>"; };
D186696C21834261002B502E /* ImageDrawingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDrawingTests.swift; sourceTree = "<group>"; };
D18B3221251852E100662F63 /* KF.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KF.swift; sourceTree = "<group>"; };
D1A1CC99219FAB4B00263AD8 /* Source.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Source.swift; sourceTree = "<group>"; };
D1A1CC9E21A0F98600263AD8 /* ImageDataProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDataProviderTests.swift; sourceTree = "<group>"; };
D1A37BDD215D34E8009B39B7 /* ImageDrawing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDrawing.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -384,6 +386,7 @@
children = (
D1A1CC98219FAB3500263AD8 /* ImageSource */,
D12AB6B1215D2BB50013BA68 /* Kingfisher.swift */,
D18B3221251852E100662F63 /* KF.swift */,
D12AB6B2215D2BB50013BA68 /* KingfisherError.swift */,
D12AB6B3215D2BB50013BA68 /* KingfisherManager.swift */,
D12AB6B4215D2BB50013BA68 /* KingfisherOptionsInfo.swift */,
Expand Down Expand Up @@ -825,6 +828,7 @@
D12AB6D4215D2BB50013BA68 /* Image.swift in Sources */,
D12AB728215D2BB50013BA68 /* String+MD5.swift in Sources */,
4B8E2917216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift in Sources */,
D18B3222251852E100662F63 /* KF.swift in Sources */,
D12AB704215D2BB50013BA68 /* Kingfisher.swift in Sources */,
D1BA781D2174D07800C69D7B /* CallbackQueue.swift in Sources */,
D12AB71C215D2BB50013BA68 /* FormatIndicatedCacheSerializer.swift in Sources */,
Expand Down
Loading