Skip to content

Commit

Permalink
Merge pull request #8 from fluttercandies/fix-ios-orientation
Browse files Browse the repository at this point in the history
Fix ios orientation
  • Loading branch information
CaiJingLong committed Oct 25, 2019
2 parents b64fda0 + 39a8bc0 commit b422e28
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 53 deletions.
Binary file added example/assets/have-exif-3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DEPENDENCIES:
- Flutter (from `.symlinks/flutter/ios`)
- image_editor (from `/Users/cai/Documents/GitHub/flutter_image_editor/ios/image_editor.podspec`)
- "image_picker (from `/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/image_picker-0.6.1+8/ios/image_picker.podspec`)"
- path_provider (from `/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/path_provider-1.3.0/ios/path_provider.podspec`)
- path_provider (from `/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/path_provider-1.4.0/ios/path_provider.podspec`)

EXTERNAL SOURCES:
Flutter:
Expand All @@ -21,13 +21,13 @@ EXTERNAL SOURCES:
image_picker:
:path: "/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/image_picker-0.6.1+8/ios/image_picker.podspec"
path_provider:
:path: "/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/path_provider-1.3.0/ios/path_provider.podspec"
:path: "/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/path_provider-1.4.0/ios/path_provider.podspec"

SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
image_editor: d0181032f68c58c9de3c9b8c17291b9a51c9dbb5
image_picker: e3eacd46b94694dde7cf2705955cece853aa1a8f
path_provider: f96fff6166a8867510d2c25fdcc346327cc4b259
path_provider: fb74bd0465e96b594bb3b5088ee4a4e7bb1f2a9d

PODFILE CHECKSUM: 1554125681f04c66fd40caade42f3011b107afb8

Expand Down
5 changes: 2 additions & 3 deletions example/lib/advanced_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _AdvancedPageState extends State<AdvancedPage> {
@override
void initState() {
super.initState();
provider = ExtendedExactAssetImageProvider(R.ASSETS_ICON_PNG);
provider = ExtendedExactAssetImageProvider(R.ASSETS_HAVE_EXIF_3_JPG);
}

@override
Expand Down Expand Up @@ -67,7 +67,7 @@ class _AdvancedPageState extends State<AdvancedPage> {
maxScale: 8.0,
cropRectPadding: EdgeInsets.all(20.0),
hitTestSize: 20.0,
cropAspectRatio: 1,
cropAspectRatio: 2 / 1,
);
},
);
Expand Down Expand Up @@ -173,7 +173,6 @@ class _AdvancedPageState extends State<AdvancedPage> {
child: SizedBox.fromSize(
size: Size.square(200),
child: Container(
color: Colors.white,
child: Image.memory(image),
),
),
Expand Down
7 changes: 4 additions & 3 deletions example/lib/const/resource.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// generate by resouce_generator library, shouldn't edit.
class R {
/// ![preview](file:///Users/cai/Documents/GitHub/flutter_image_editor/example/./assets/have-exif-8.jpg)
static const String ASSETS_HAVE_EXIF_8_JPG = "assets/have-exif-8.jpg";


/// ![preview](file:///Users/cai/Documents/GitHub/flutter_image_editor/example/./assets/have-exif-3.jpg)
static const String ASSETS_HAVE_EXIF_3_JPG = "assets/have-exif-3.jpg";

/// ![preview](file:///Users/cai/Documents/GitHub/flutter_image_editor/example/./assets/icon.png)
static const String ASSETS_ICON_PNG = "assets/icon.png";
}
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ packages:
name: extended_image
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.3"
version: "0.6.4"
extended_image_library:
dependency: transitive
description:
name: extended_image_library
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.7"
version: "0.1.8"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -164,7 +164,7 @@ packages:
name: path_provider
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
version: "1.4.0"
pedantic:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
oktoast: ^2.2.0
extended_image: ^0.6.3
extended_image: ^0.6.4
image_picker: ^0.6.1+8

dev_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/EditorOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ struct RotateOption: FlutterImageEditorOption {
struct FormatOption {
var format: Int
var quality: Int
}
}
1 change: 1 addition & 0 deletions ios/Classes/FlutterImageEditorPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ @implementation FlutterImageEditorPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftFlutterImageEditorPlugin registerWithRegistrar:registrar];
}

@end
1 change: 1 addition & 0 deletions ios/Classes/UIImage+crop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension UIImage {
guard let cg = self.cgImage?.cropping(to: CGRect(x: x, y: y, width: width, height: height)) else {
return self
}

return UIImage(cgImage: cg)
}
}
29 changes: 29 additions & 0 deletions ios/Classes/UIImage+fix_orientation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UIImage+fix_orientation.swift
// image_editor
//
// Created by Caijinglong on 2019/10/24.
//

import Foundation

extension UIImage {
func fixOrientation() -> UIImage {
if imageOrientation == .up {
return self
}

UIGraphicsBeginImageContextWithOptions(size, false, scale)

draw(in: CGRect(origin: .zero, size: size))

guard let result = UIGraphicsGetImageFromCurrentImageContext() else {
UIGraphicsEndImageContext()
return self
}

UIGraphicsEndImageContext()

return result
}
}
7 changes: 5 additions & 2 deletions ios/Classes/UIImage+flip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import UIKit

extension UIImage {
func flip(horizontal: Bool, vertical: Bool) -> UIImage {
if !horizontal, !vertical {
return self
}
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 1)
guard let ctx = UIGraphicsGetCurrentContext() else {
Expand All @@ -28,7 +31,7 @@ extension UIImage {
} else if vertical, !horizontal {
// 仅垂直翻转, 和 CG 体系的默认情况一致, 不操作
} else if vertical, horizontal {
// 水平+垂直, 因为默认情况下是垂直翻转, 这里仅水平翻转即可
// 水平+垂直, 因为默认情况下是垂直翻转, 这里仅水平翻转即可, 虽然这么写,但是实际上开头就直接返回self
ctx.translateBy(x: rect.size.width, y: 0)
ctx.scaleBy(x: -1, y: 1)
} else {
Expand Down Expand Up @@ -57,4 +60,4 @@ extension Double {
func toCGFloat() -> CGFloat {
return CGFloat(self)
}
}
}
28 changes: 16 additions & 12 deletions ios/Classes/UIImage+rotate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ import Foundation

extension UIImage {
func rotate(_ angle: Int) -> UIImage {
let radian = CGFloat(angle) * CGFloat.pi / 180

UIGraphicsBeginImageContext(size)
guard let ctx = UIGraphicsGetCurrentContext(), let oldCgImage = cgImage else {
return self
}

// 因为如果旋转角度不是90的倍数, 则图片尺寸会发生变化, 这里使用UIView作为工具测量出旋转后的尺寸,
let radian = CGFloat(angle) * CGFloat.pi / 180
let measureView = UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let t = CGAffineTransform(rotationAngle: radian)
measureView.transform = t
let rect = measureView.frame

// 使用CG开始绘制
UIGraphicsBeginImageContext(rect.size)
guard let ctx = UIGraphicsGetCurrentContext() else {
return self
}

let origin = CGPoint(x: rect.width / 2.0,
y: rect.height / 2.0)

// 开始绘制
ctx.translateBy(x: rect.width / 2, y: rect.height / 2)
ctx.translateBy(x: origin.x, y: origin.y)
ctx.rotate(by: radian)
ctx.scaleBy(x: 1, y: -1)
ctx.draw(oldCgImage, in: CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height))
draw(in: CGRect(x: -origin.y, y: -origin.x, width: size.width, height: size.height))

guard let cgImg = UIGraphicsGetImageFromCurrentImageContext()?.cgImage else {
guard let rotatedImage = UIGraphicsGetImageFromCurrentImageContext() else {
UIGraphicsEndImageContext()
return self
}
UIGraphicsEndImageContext()
return UIImage(cgImage: cgImg, scale: 1, orientation: imageOrientation)

return rotatedImage
}
}
28 changes: 3 additions & 25 deletions ios/Classes/UIImageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,8 @@ class UIImageHandler {
self.image = image
}

func handleImage(options: [FlutterImageEditorOption], fixOrientation: Bool = true) {
if fixOrientation {
let orientation = image.imageOrientation
switch orientation {
case .up:
break
case .down:
image = image.rotate(180)
case .left:
image = image.rotate(270)
case .right:
image = image.rotate(90)
case .upMirrored:
image = image.flip(horizontal: true, vertical: false)
case .downMirrored:
image = image.flip(horizontal: false, vertical: true)
case .leftMirrored:
image = image.rotate(90)
image = image.flip(horizontal: true, vertical: false)
case .rightMirrored:
image = image.rotate(270)
image = image.flip(horizontal: true, vertical: false)
}
}
func handleImage(options: [FlutterImageEditorOption], fixOrientation _: Bool = true) {
image = image.fixOrientation()

for option in options {
if option is FlipOption {
Expand Down Expand Up @@ -72,4 +50,4 @@ class UIImageHandler {
private func handleFlip(_ option: FlipOption) -> UIImage {
return image.flip(horizontal: option.horizontal, vertical: option.vertical)
}
}
}

0 comments on commit b422e28

Please sign in to comment.