Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit 151be0e

Browse files
author
Chris Combs
committed
removing force unwraps
1 parent a6ddf12 commit 151be0e

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

Codemine/Extensions/UIImage+Utilities.swift

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,37 @@ public extension UIImage {
2424

2525
- Returns: A 'UIImage' with the specified color, size and corner radius.
2626
*/
27-
public class func imageFromColor(color: UIColor, size: CGSize, cornerRadius: CGFloat) -> UIImage {
27+
public class func imageFromColor(color: UIColor, size: CGSize, cornerRadius: CGFloat) -> UIImage? {
2828

2929
/// The base rectangle of the image.
3030
let rect = CGRectMake(0, 0, size.width, size.height)
3131
UIGraphicsBeginImageContext(rect.size)
3232

33-
/// The graphics context of the image.
34-
let context = UIGraphicsGetCurrentContext()
35-
CGContextSetFillColorWithColor(context!, color.CGColor)
36-
CGContextFillRect(context!, rect)
37-
38-
/// Image that will be retured.
39-
var image = UIGraphicsGetImageFromCurrentImageContext()
40-
UIGraphicsEndImageContext()
41-
42-
UIGraphicsBeginImageContext(size)
43-
44-
UIBezierPath(roundedRect: rect, cornerRadius:cornerRadius).addClip()
45-
image! .drawInRect(rect)
46-
47-
image = UIGraphicsGetImageFromCurrentImageContext()
48-
UIGraphicsEndImageContext()
49-
50-
return image!
33+
/// The graphics context of the image.
34+
if let context = UIGraphicsGetCurrentContext() {
35+
CGContextSetFillColorWithColor(context, color.CGColor)
36+
CGContextFillRect(context, rect)
37+
38+
/// Image that will be retured.
39+
var image = UIGraphicsGetImageFromCurrentImageContext()
40+
UIGraphicsEndImageContext()
41+
42+
UIGraphicsBeginImageContext(size)
43+
44+
UIBezierPath(roundedRect: rect, cornerRadius:cornerRadius).addClip()
45+
image?.drawInRect(rect)
46+
47+
image = UIGraphicsGetImageFromCurrentImageContext()
48+
UIGraphicsEndImageContext()
49+
50+
return image
51+
}
52+
return nil
5153
}
52-
54+
5355
/**
5456
Embed an icon/image on top of a background image.
55-
57+
5658
`imageOne` will be the background and `icon` is the image that will be on top of `imageOne`.
5759
The `UIImage` that is set with the parameter `icon` will be centered on `imageOne`.
5860

@@ -61,7 +63,7 @@ public extension UIImage {
6163
- icon: The embedded image that will be on top.
6264
- Returns: The combined image as `UIImage`.
6365
*/
64-
public class func imageByEmbeddingIconIn(imageOne: UIImage, icon: UIImage) -> UIImage {
66+
public class func imageByEmbeddingIconIn(imageOne: UIImage, icon: UIImage) -> UIImage? {
6567
let newSize = CGSizeMake(imageOne.size.width, imageOne.size.height)
6668
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
6769

@@ -70,7 +72,7 @@ public extension UIImage {
7072
// Center icon
7173
icon.drawInRect(CGRectMake(imageOne.size.width/2 - icon.size.width/2, imageOne.size.height/2 - icon.size.height/2, icon.size.width, icon.size.height), blendMode:CGBlendMode.Normal, alpha:1.0)
7274

73-
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
75+
let newImage = UIGraphicsGetImageFromCurrentImageContext()
7476
return newImage
7577
}
7678

@@ -80,13 +82,13 @@ public extension UIImage {
8082

8183
- Returns: The orientation corrected image as an `UIImage`.
8284
*/
83-
public func rotationCorrectedImage() -> UIImage {
85+
public func rotationCorrectedImage() -> UIImage? {
8486
// if (self.imageOrientation == UIImageOrientation.Up) { return self }
8587

86-
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
88+
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
8789
self.drawInRect(CGRect(origin: CGPointZero, size: self.size))
88-
let normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
89-
UIGraphicsEndImageContext();
90-
return normalizedImage!;
90+
let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
91+
UIGraphicsEndImageContext()
92+
return normalizedImage
9193
}
9294
}

Codemine/Extensions/UIView+Utilities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public extension UIView {
1717
- Parameter nibName: The name that the UIView will get as its `nibName` assigned as a `String`.
1818
- Returns: `Generics type`.
1919
*/
20-
public static func viewWithNibNamed<T>(nibName:String) -> T {
21-
let view = UINib(nibName: nibName, bundle: nil).instantiateWithOwner(nil, options: nil).first! as! T
20+
public static func viewWithNibNamed<T>(nibName:String) -> T? {
21+
let view = UINib(nibName: nibName, bundle: nil).instantiateWithOwner(nil, options: nil).first as? T
2222
return view
2323
}
2424

0 commit comments

Comments
 (0)