Skip to content

Commit 4b3adec

Browse files
committed
Fix crop issue
1 parent b8c52a1 commit 4b3adec

File tree

4 files changed

+110
-51
lines changed

4 files changed

+110
-51
lines changed

SCNVideoWriter/Classes/Error.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Error.swift
3+
// Pods-SCNVideoWriter_Example
4+
//
5+
// Created by Tomoya Hirano on 2017/08/20.
6+
//
7+
8+
import UIKit
9+
10+
extension SCNVideoWriter {
11+
public struct VideoSizeError: Error {}
12+
}
13+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Options.swift
3+
// Pods-SCNVideoWriter_Example
4+
//
5+
// Created by Tomoya Hirano on 2017/08/20.
6+
//
7+
8+
import UIKit
9+
import AVFoundation
10+
11+
extension SCNVideoWriter {
12+
public struct Options {
13+
public var timeScale: Int32
14+
public var renderSize: CGSize
15+
public var videoSize: CGSize
16+
public var fps: Int
17+
public var outputUrl: URL
18+
public var fileType: String
19+
public var codec: String
20+
public var deleteFileIfExists: Bool
21+
22+
public static var `default`: Options {
23+
return Options(timeScale: 600,
24+
renderSize: CGSize(width: 640, height: 640),
25+
videoSize: CGSize(width: 1280, height: 720),
26+
fps: 60,
27+
outputUrl: URL(fileURLWithPath: NSTemporaryDirectory() + "output.mp4"),
28+
fileType: AVFileTypeAppleM4V,
29+
codec: AVVideoCodecH264,
30+
deleteFileIfExists: true)
31+
}
32+
33+
var assetWriterInputSettings: [String : Any] {
34+
return [
35+
AVVideoCodecKey: codec,
36+
AVVideoWidthKey: videoSize.width,
37+
AVVideoHeightKey: videoSize.height
38+
]
39+
}
40+
var sourcePixelBufferAttributes: [String : Any] {
41+
return [
42+
kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_32ARGB),
43+
kCVPixelBufferWidthKey as String: videoSize.width,
44+
kCVPixelBufferHeightKey as String: videoSize.height,
45+
]
46+
}
47+
}
48+
}
49+

SCNVideoWriter/Classes/SCNVideoWriter.swift

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class SCNVideoWriter {
4343
fileType: options.fileType)
4444
self.input = AVAssetWriterInput(mediaType: AVMediaTypeVideo,
4545
outputSettings: options.assetWriterInputSettings)
46-
//input.expectsMediaDataInRealTime = true
4746
self.pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input,
4847
sourcePixelBufferAttributes: options.sourcePixelBufferAttributes)
4948
prepare(with: options)
@@ -102,7 +101,7 @@ public class SCNVideoWriter {
102101
autoreleasepool {
103102
currentTime = CFAbsoluteTimeGetCurrent() - initialTime
104103
let image = renderer.snapshot(atTime: currentTime, with: renderSize, antialiasingMode: .multisampling4X)
105-
let croppedImage = image.crop(at: videoSize)
104+
guard let croppedImage = image.fill(at: videoSize) else { return }
106105
guard let pixelBuffer = PixelBufferFactory.make(with: videoSize, from: croppedImage, usingBuffer: pool) else { return }
107106
let value: Int64 = Int64(currentTime * CFTimeInterval(options.timeScale))
108107
let presentationTime = CMTimeMake(value, options.timeScale)
@@ -117,41 +116,4 @@ public class SCNVideoWriter {
117116
}
118117
}
119118

120-
extension SCNVideoWriter {
121-
public struct Options {
122-
public var timeScale: Int32
123-
public var renderSize: CGSize
124-
public var videoSize: CGSize
125-
public var fps: Int
126-
public var outputUrl: URL
127-
public var fileType: String
128-
public var codec: String
129-
public var deleteFileIfExists: Bool
130-
131-
public static var `default`: Options {
132-
return Options(timeScale: 600,
133-
renderSize: CGSize(width: 640, height: 640),
134-
videoSize: CGSize(width: 640, height: 640),
135-
fps: 60,
136-
outputUrl: URL(fileURLWithPath: NSTemporaryDirectory() + "output.mp4"),
137-
fileType: AVFileTypeAppleM4V,
138-
codec: AVVideoCodecH264,
139-
deleteFileIfExists: true)
140-
}
141-
142-
var assetWriterInputSettings: [String : Any] {
143-
return [
144-
AVVideoCodecKey: codec,
145-
AVVideoWidthKey: videoSize.width,
146-
AVVideoHeightKey: videoSize.height
147-
]
148-
}
149-
var sourcePixelBufferAttributes: [String : Any] {
150-
return [
151-
kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_32ARGB),
152-
kCVPixelBufferWidthKey as String: videoSize.width,
153-
kCVPixelBufferHeightKey as String: videoSize.height
154-
]
155-
}
156-
}
157-
}
119+

SCNVideoWriter/Classes/UIImage+Extensions.swift

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,52 @@
88
import UIKit
99

1010
extension UIImage {
11-
func crop(at size: CGSize) -> UIImage {
11+
func fill(at targetSize: CGSize) -> UIImage? {
1212
let imageSize = self.size
13-
let cropSize = size
14-
let x = (imageSize.width - cropSize.width) / 2.0
15-
let y = (imageSize.height - cropSize.height) / 2.0
16-
let cropRect = CGRect(x: x, y: y, width: size.width, height: size.height)
17-
return crop(to: cropRect)
18-
}
19-
20-
func crop(to rect: CGRect) -> UIImage {
21-
guard let croppedImageRef = cgImage?.cropping(to: rect) else { return UIImage() }
22-
return UIImage(cgImage: croppedImageRef, scale: scale, orientation: .up)
13+
let width = imageSize.width
14+
let height = imageSize.height
15+
let targetWidth = targetSize.width
16+
let targetHeight = targetSize.height
17+
var scaleFactor: CGFloat = 0.0
18+
var scaledWidth = targetWidth
19+
var scaledHeight = targetHeight
20+
var thumbnailPoint = CGPoint(x: 0, y: 0)
21+
22+
if imageSize != targetSize {
23+
let widthFactor = targetWidth / width
24+
let heightFactor = targetHeight / height
25+
26+
if widthFactor > heightFactor {
27+
scaleFactor = widthFactor
28+
} else {
29+
scaleFactor = heightFactor
30+
}
31+
32+
scaledWidth = width * scaleFactor
33+
scaledHeight = height * scaleFactor
34+
35+
if widthFactor > heightFactor {
36+
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5
37+
} else {
38+
if widthFactor < heightFactor {
39+
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5
40+
}
41+
}
42+
}
43+
44+
UIGraphicsBeginImageContext(targetSize)
45+
46+
var thumbnailRect = CGRect.zero
47+
thumbnailRect.origin = thumbnailPoint
48+
thumbnailRect.size.width = scaledWidth
49+
thumbnailRect.size.height = scaledHeight
50+
51+
draw(in: thumbnailRect)
52+
53+
let newImage = UIGraphicsGetImageFromCurrentImageContext()
54+
55+
UIGraphicsEndImageContext()
56+
57+
return newImage
2358
}
2459
}

0 commit comments

Comments
 (0)