Skip to content

Commit

Permalink
[ios_platform_images] migrate objC to swift (flutter#4847)
Browse files Browse the repository at this point in the history
`ios_platform_images` part of flutter/flutter#119101

Migrate ios_platform_images to Swift.
  • Loading branch information
Mairramer authored and HugoOlthof committed Dec 13, 2023
1 parent 6708ff3 commit 795b4a5
Show file tree
Hide file tree
Showing 22 changed files with 402 additions and 392 deletions.
1 change: 1 addition & 0 deletions packages/ios_platform_images/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]>
Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
Mairramer <[email protected]>
4 changes: 4 additions & 0 deletions packages/ios_platform_images/CHANGELOG.md
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.
Expand Down
12 changes: 6 additions & 6 deletions packages/ios_platform_images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ Widget build(BuildContext context) {
}
```

`IosPlatformImages.load` functions like [[UIImage imageNamed:]](https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed).
`IosPlatformImages.load` works similarly to [`UIImage(named:)`](https://developer.apple.com/documentation/uikit/uiimage/1624146-imagenamed).

### Flutter->iOS Example

```objc
#import <ios_platform_images/UIImage+ios_platform_images.h>
```swift
import ios_platform_images

static UIImageView* MakeImage() {
UIImage* image = [UIImage flutterImageWithName:@"assets/foo.png"];
return [[UIImageView alloc] initWithImage:image];
func makeImage() -> UIImageView {
let image = UIImage.flutterImageWithName("assets/foo.png")
return UIImageView(image: image)
}
```

Large diffs are not rendered by default.

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>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F76AC1BD266713D00040C8BC"
BuildableName = "RunnerTests.xctest"
BlueprintName = "RunnerTests"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import UIKit
import Flutter
import UIKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
Expand Down

This file was deleted.

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 packages/ios_platform_images/ios/Classes/IosPlatformImagesPlugin.h

This file was deleted.

40 changes: 0 additions & 40 deletions packages/ios_platform_images/ios/Classes/IosPlatformImagesPlugin.m

This file was deleted.

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
}

}

This file was deleted.

This file was deleted.

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)
}
}
47 changes: 0 additions & 47 deletions packages/ios_platform_images/ios/Classes/messages.g.h

This file was deleted.

Loading

0 comments on commit 795b4a5

Please sign in to comment.