forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios_platform_images] migrate objC to swift (flutter#4847)
`ios_platform_images` part of flutter/flutter#119101 Migrate ios_platform_images to Swift.
- Loading branch information
1 parent
6708ff3
commit 795b4a5
Showing
22 changed files
with
402 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]> | |
Anton Borries <[email protected]> | ||
Alex Li <[email protected]> | ||
Rahul Raj <[email protected]> | ||
Mairramer <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.2.3 | ||
|
||
* Migrates to a Swift implementation. | ||
|
||
## 0.2.2+3 | ||
|
||
* Converts platform communication to Pigeon. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 70 additions & 43 deletions
113
packages/ios_platform_images/example/ios/Runner.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...es/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
packages/ios_platform_images/example/ios/RunnerTests/IosPlatformImagesTests.m
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
packages/ios_platform_images/example/ios/RunnerTests/IosPlatformImagesTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import Flutter | ||
import XCTest | ||
|
||
@testable import ios_platform_images | ||
|
||
class IosPlatformImagesTests: XCTestCase { | ||
let plugin = IosPlatformImagesPlugin() | ||
|
||
func testLoadImage() { | ||
let assetName = "flutter" | ||
let imageData = plugin.loadImage(name: assetName) | ||
|
||
XCTAssertNotNil(imageData) | ||
XCTAssertNotNil(imageData?.data) | ||
} | ||
|
||
func testLoadImageNotFound() { | ||
let assetName = "notFound" | ||
let imageData = plugin.loadImage(name: assetName) | ||
|
||
XCTAssertNil(imageData) | ||
} | ||
|
||
func testResolveURL() { | ||
let resourceName = "textfile" | ||
do { | ||
let url = try plugin.resolveUrl(resourceName: resourceName, extension: nil) | ||
XCTAssertNotNil(url) | ||
XCTAssertTrue(url?.contains(resourceName) ?? false) | ||
} catch { | ||
XCTFail("Error while resolving URL: \(error)") | ||
} | ||
} | ||
|
||
func testResolveURLNotFound() { | ||
do { | ||
let url = try plugin.resolveUrl(resourceName: "notFound", extension: nil) | ||
XCTAssertNil(url) | ||
} catch { | ||
XCTFail("Error while resolving URL: \(error)") | ||
} | ||
} | ||
|
||
} |
12 changes: 0 additions & 12 deletions
12
packages/ios_platform_images/ios/Classes/IosPlatformImagesPlugin.h
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
packages/ios_platform_images/ios/Classes/IosPlatformImagesPlugin.m
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
packages/ios_platform_images/ios/Classes/IosPlatformImagesPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import Flutter | ||
import Foundation | ||
|
||
public final class IosPlatformImagesPlugin: NSObject, FlutterPlugin, PlatformImagesApi { | ||
public static func register(with registrar: FlutterPluginRegistrar) { | ||
let instance = IosPlatformImagesPlugin() | ||
let messenger = registrar.messenger() | ||
PlatformImagesApiSetup.setUp(binaryMessenger: messenger, api: instance) | ||
} | ||
|
||
func loadImage(name: String) -> PlatformImageData? { | ||
guard let image = UIImage(named: name), | ||
let data = image.pngData() | ||
else { | ||
return nil | ||
} | ||
|
||
return PlatformImageData( | ||
data: FlutterStandardTypedData(bytes: data), scale: Double(image.scale)) | ||
} | ||
|
||
func resolveUrl(resourceName: String, extension: String?) throws -> String? { | ||
guard | ||
let url = Bundle.main.url( | ||
forResource: resourceName, | ||
withExtension: `extension`) | ||
else { | ||
return nil | ||
} | ||
|
||
return url.absoluteString | ||
} | ||
|
||
} |
27 changes: 0 additions & 27 deletions
27
packages/ios_platform_images/ios/Classes/UIImage+ios_platform_images.h
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
packages/ios_platform_images/ios/Classes/UIImage+ios_platform_images.m
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
packages/ios_platform_images/ios/Classes/UIImageIosPlatformImages.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import Flutter | ||
import Foundation | ||
import UIKit | ||
|
||
@objc extension UIImage { | ||
/// Loads a UIImage from the embedded Flutter project's assets. | ||
/// | ||
/// This method loads the Flutter asset that is appropriate for the current | ||
/// screen. If you are on a 2x retina device where usually `UIImage` would be | ||
/// loading `@2x` assets, it will attempt to load the `2.0x` variant. It will | ||
/// load the standard image if it can't find the `2.0x` variant. | ||
/// | ||
/// For example, if your Flutter project's `pubspec.yaml` lists "assets/foo.png" | ||
/// and "assets/2.0x/foo.png", calling | ||
/// `[UIImage flutterImageWithName:@"assets/foo.png"]` will load | ||
/// "assets/2.0x/foo.png". | ||
/// | ||
/// See also https://flutter.dev/docs/development/ui/assets-and-images | ||
/// | ||
/// Note: We don't yet support images from package dependencies (ex. | ||
/// `AssetImage('icons/heart.png', package: 'my_icons')`). | ||
public static func flutterImageWithName(_ name: String) -> UIImage? { | ||
let filename = (name as NSString).lastPathComponent | ||
let path = (name as NSString).deletingLastPathComponent | ||
|
||
for screenScale in stride(from: Int(UIScreen.main.scale), to: 1, by: -1) { | ||
// TODO(hellohuanlin): Fix duplicate slashes in this path construction. | ||
let key = FlutterDartProject.lookupKey(forAsset: "\(path)/\(screenScale).0x/\(filename)") | ||
if let image = UIImage(named: key, in: Bundle.main, compatibleWith: nil) { | ||
return image | ||
} | ||
} | ||
|
||
let key = FlutterDartProject.lookupKey(forAsset: name) | ||
return UIImage(named: key, in: Bundle.main, compatibleWith: nil) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.