-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path0631A15B-7467-4DB2-A740-0E04F1D1EFF6.codesnippet
More file actions
60 lines (59 loc) · 2.25 KB
/
0631A15B-7467-4DB2-A740-0E04F1D1EFF6.codesnippet
File metadata and controls
60 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
<key>IDECodeSnippetContents</key>
<string>extension UIImage {
func cropImageIfNeed(_ maxSize: CGSize) -> UIImage {
let isTooBig = size.width > maxSize.width || size.height > maxSize.height
if isTooBig {
let
maxRect = CGRect(origin: CGPoint.zero, size: maxSize),
scaledRect = AVMakeRect(aspectRatio: self.size, insideRect: maxRect),
scaledSize = scaledRect.size,
targetRect = CGRect(origin: CGPoint.zero, size: scaledSize),
width = Int(scaledSize.width),
height = Int(scaledSize.height),
cgImage = self.cgImage,
bitsPerComp = cgImage?.bitsPerComponent,
compsPerPixel = 4, // RGBA
bytesPerRow = width * compsPerPixel,
colorSpace = cgImage?.colorSpace,
bitmapInfo = cgImage?.bitmapInfo,
context = CGContext(
data: nil,
width: width,
height: height,
bitsPerComponent: bitsPerComp!,
bytesPerRow: bytesPerRow,
space: colorSpace!,
bitmapInfo: (bitmapInfo?.rawValue)!)
if context != nil {
context!.interpolationQuality = CGInterpolationQuality.low
context?.draw(cgImage!, in: targetRect)
if let scaledCGImage = context?.makeImage() {
return UIImage(cgImage: scaledCGImage)
}
}
}
return self
}
}</string>
<key>IDECodeSnippetIdentifier</key>
<string>0631A15B-7467-4DB2-A740-0E04F1D1EFF6</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Generic</string>
<key>IDECodeSnippetSummary</key>
<string>ThangTM</string>
<key>IDECodeSnippetTitle</key>
<string>Swift 4: Crop Image using AVMakeRect</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>