Skip to content

Commit

Permalink
0.2.4: 1.优化代码;2.正确获取视图信息(主线程获取)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogue24 committed Oct 26, 2023
1 parent ed3a5ee commit b3723df
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 280 deletions.
17 changes: 12 additions & 5 deletions Example/JPCrop/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private extension CropViewController {
left: 15.px,
bottom: 50.px + NavBarH + DiffTabBarH + 15.px,
right: 15.px)
let croper = Croper(frame: PortraitScreenBounds, configure)
let croper = Croper(frame: PortraitScreenBounds, configure: configure)
croper.clipsToBounds = false
view.insertSubview(croper, at: 0)
self.croper = croper
Expand Down Expand Up @@ -157,10 +157,17 @@ private extension CropViewController {
navigationController?.popViewController(animated: true)

guard let cropDone = self.cropDone else { return }
let configure = croper.syncConfigure()
croper.asyncCrop {
guard let image = $0 else { return }
cropDone(image, configure)

DispatchQueue.global().async {
guard let image = self.croper.crop() else {
JPrint("裁剪失败!")
return
}

let configure = self.croper.getCurrentConfigure()
DispatchQueue.main.async {
cropDone(image, configure)
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- JPCrop (0.2.3)
- JPCrop (0.2.4)
- pop (1.0.12)

DEPENDENCIES:
Expand All @@ -15,9 +15,9 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
JPCrop: d260e4456ee0b281214642f6abd96b76b3e17971
JPCrop: 3a5b56acc7c0865312540c9738ae4253cee3fa2b
pop: d582054913807fd11fd50bfe6a539d91c7e1a55a

PODFILE CHECKSUM: 2370da9e45a5c7299c50a99d73814c8e01e456f7

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
6 changes: 3 additions & 3 deletions Example/Pods/Local Podspecs/JPCrop.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

338 changes: 172 additions & 166 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Example/Pods/Target Support Files/JPCrop/JPCrop-Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/Pods/Target Support Files/pop/pop-Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Example/Pods/Target Support Files/pop/pop.debug.xcconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Example/Pods/Target Support Files/pop/pop.release.xcconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public extension Croper {
/// - compressScale: 压缩比例,默认为1,即原图尺寸
func asyncCrop(_ compressScale: CGFloat = 1, _ cropDone: @escaping (UIImage?) -> ()) {
guard let imageRef = image.cgImage else {
cropDone(nil)
DispatchQueue.main.async { cropDone(nil) }
return
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
// Created by Rogue24 on 2022/3/5.
//

import UIKit

extension Croper {
var isLandscapeImage: Bool { imageWHRatio > 1 }
var isLandscapeImage: Bool {
imageWHRatio > 1
}

func scaleValue(_ t: CGAffineTransform) -> CGFloat { sqrt(t.a * t.a + t.c * t.c) }
func scaleValue(_ t: CGAffineTransform) -> CGFloat {
sqrt(t.a * t.a + t.c * t.c)
}

func rotate(_ angle: CGFloat, isAutoZoom: Bool, animated: Bool) {
guard animated else {
rotate(angle, isAutoZoom: isAutoZoom)
return
}

UIView.animate(withDuration: Self.animDuration, delay: 0, options: .curveEaseOut) {
self.rotate(angle, isAutoZoom: isAutoZoom)
}
Expand Down Expand Up @@ -45,10 +52,12 @@ extension Croper {
}

func updateGrid(_ idleGridAlpha: Float, _ rotateGridAlpha: Float, animated: Bool = false) {

if animated {
buildAnimation(addTo: idleGridLayer, "opacity", idleGridAlpha, 0.12, timingFunctionName: .easeIn)
buildAnimation(addTo: rotateGridLayer, "opacity", rotateGridAlpha, 0.12, timingFunctionName: .easeIn)
}

CATransaction.begin()
CATransaction.setDisableActions(true)
idleGridLayer.opacity = idleGridAlpha
Expand All @@ -59,12 +68,7 @@ extension Croper {

extension Croper {
/// 更新裁剪宽高比,并获取改变前后的差值
func resetCropWHRatio(_ whRatio: CGFloat) -> (
factor: RotateFactor,
contentScalePoint: CGPoint,
zoomScale: CGFloat,
imageFrameSize: CGSize
) {
func resetCropWHRatio(_ whRatio: CGFloat) -> DiffFactor {
cropWHRatio = fitCropWHRatio(whRatio, isCallBack: true)

// 更新可裁剪区域
Expand Down Expand Up @@ -99,7 +103,10 @@ extension Croper {
let imageFrameSize = CGSize(width: imageBoundsSize.width * zoomScale,
height: imageBoundsSize.height * zoomScale)

return (factor, contentScalePoint, zoomScale, imageFrameSize)
return .init(factor: factor,
contentScalePoint: contentScalePoint,
zoomScale: zoomScale,
imageFrameSize: imageFrameSize)
}

/// 无痕刷新 scrollView 改变后的 transform 和其他差值(让 scrollView 形变后相对于之前的 UI 状态“看上去”没有变化一样)
Expand Down Expand Up @@ -171,67 +178,3 @@ extension Croper {
CATransaction.commit()
}
}

extension Croper {
static func crop(_ compressScale: CGFloat,
_ imageRef: CGImage,
_ cropWHRatio: CGFloat,
_ scale: CGFloat,
_ convertTranslate: CGPoint,
_ radian: CGFloat,
_ imageBoundsHeight: CGFloat) -> UIImage? {

let width = CGFloat(imageRef.width) * compressScale
let height = CGFloat(imageRef.height) * compressScale

// 获取裁剪尺寸和裁剪区域
var rendSize: CGSize
if width > height {
rendSize = CGSize(width: height * cropWHRatio, height: height)
if rendSize.width > width {
rendSize = CGSize(width: width, height: width / cropWHRatio)
}
} else {
rendSize = CGSize(width: width, height: width / cropWHRatio)
if rendSize.height > height {
rendSize = CGSize(width: height * cropWHRatio, height: height)
}
}

var bitmapRawValue = CGBitmapInfo.byteOrder32Little.rawValue
let alphaInfo = imageRef.alphaInfo
if alphaInfo == .premultipliedLast ||
alphaInfo == .premultipliedFirst ||
alphaInfo == .last ||
alphaInfo == .first {
bitmapRawValue += CGImageAlphaInfo.premultipliedFirst.rawValue
} else {
bitmapRawValue += CGImageAlphaInfo.noneSkipFirst.rawValue
}

guard let context = CGContext(data: nil,
width: Int(rendSize.width),
height: Int(rendSize.height),
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: bitmapRawValue) else { return nil }
context.setShouldAntialias(true)
context.setAllowsAntialiasing(true)
context.interpolationQuality = .high

let iScale = CGFloat(imageRef.height) / (imageBoundsHeight * scale)
var translate = convertTranslate
translate.y = imageBoundsHeight - translate.y // 左下点与底部的距离
translate.x *= -1 * scale * iScale
translate.y *= -1 * scale * iScale

let transform = CGAffineTransform(scaleX: scale, y: scale).rotated(by: -radian).translatedBy(x: translate.x, y: translate.y)
context.concatenate(transform)

context.draw(imageRef, in: CGRect(origin: .zero, size: CGSize(width: width, height: height)))

guard let newImageRef = context.makeImage() else { return nil }
return UIImage(cgImage: newImageRef)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import UIKit

public extension Croper {
typealias RotateFactor = (scale: CGFloat, transform: CGAffineTransform, contentInset: UIEdgeInsets)

/// 旋转基准角度:0°/360°、90°、180°、270°
enum OriginAngle: CGFloat {
/// 以 0°/360° 为基准,可旋转范围:`-45° ~ 45°`
Expand Down Expand Up @@ -45,6 +43,7 @@ public extension Croper {
}
}

/// 初始化配置
struct Configure {
/// 裁剪图片
public let image: UIImage
Expand Down Expand Up @@ -79,3 +78,26 @@ public extension Croper {
}
}
}

extension Croper {
struct RotateFactor {
let scale: CGFloat
let transform: CGAffineTransform
let contentInset: UIEdgeInsets
}

struct DiffFactor {
let factor: RotateFactor
let contentScalePoint: CGPoint
let zoomScale: CGFloat
let imageFrameSize: CGSize
}

struct CropFactor {
let cropWHRatio: CGFloat
let scale: CGFloat
let convertTranslate: CGPoint
let radian: CGFloat
let imageBoundsHeight: CGFloat
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ extension Croper {
layer.addSublayer(borderLayer)
}

func buildGridPath(_ gridCount: (verCount: Int, horCount: Int)) -> UIBezierPath {
let gridPath = UIBezierPath()
guard gridCount.verCount > 1, gridCount.horCount > 1 else { return gridPath }
let verSpace = cropFrame.height / CGFloat(gridCount.verCount)
let horSpace = cropFrame.width / CGFloat(gridCount.horCount)
for i in 1 ..< gridCount.verCount {
let px = cropFrame.origin.x
let py = cropFrame.origin.y + verSpace * CGFloat(i)
gridPath.move(to: CGPoint(x: px, y: py))
gridPath.addLine(to: CGPoint(x: px + cropFrame.width, y: py))
}
for i in 1 ..< gridCount.horCount {
let px = cropFrame.origin.x + horSpace * CGFloat(i)
let py = cropFrame.origin.y
gridPath.move(to: CGPoint(x: px, y: py))
gridPath.addLine(to: CGPoint(x: px, y: py + cropFrame.height))
}
return gridPath
}

func buildAnimation<T>(addTo layer: CALayer, _ keyPath: String, _ toValue: T, _ duration: TimeInterval, timingFunctionName: CAMediaTimingFunctionName = .easeOut) {
let anim = CABasicAnimation(keyPath: keyPath)
Expand Down

0 comments on commit b3723df

Please sign in to comment.