在tableviewcell中从字符串创建图像时崩溃

时间:2018-05-30 07:32:31

标签: swift string image uitableview uiimage

我在cellForRowAtIndexPath中有以下内容并收到崩溃报告:

let item = audits[indexPath.row]
if let emp = Helper_Employee.getEmployee(id: item.auditor){
   cell.nameLbl.text = "\(emp.surname), \(emp.firstname)"+" - \(item.auditnum)"
   if let image = emp.profilepicture.fromBase64Img()
   {
      // the if below is where crashlytics is pointing to
      if let img = UIImage(data: image as Data) {
         cell.empImg.image = img
      } else {
         cell.empImg.image = #imageLiteral(resourceName: "ic_blank_employee")
      }
   } else {
          cell.empImg.image = #imageLiteral(resourceName: "ic_blank_employee")
   }
} 
else {
      cell.empImg.image = #imageLiteral(resourceName: "ic_blank_employee")
      cell.nameLbl.text = "No Employee"
}

这是来自base64Image:

func fromBase64Img() -> NSData? {
        guard let data = NSData(base64Encoded: self, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
            return nil
        }

        return data
    }

崩溃报告:

Crashed: com.apple.main-thread
0  AppName                        0xaefa50 specialized L_AuditsVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (L_AuditsVC.swift:186)
1  AppName                        0xaeee38 specialized L_AuditsVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (L_AuditsVC.swift:186)
2  AppName                        0xaec91c @objc L_AuditsVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (L_AuditsVC.swift)
3  UIKit                          0x263ad465 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 672
4  UIKit                          0x263ad5a5 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 68
5  UIKit                          0x2639cb51 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2272
6  UIKit                          0x263b1d1b -[UITableView _performWithCachedTraitCollection:] + 78
7  UIKit                          0x2615123b -[UITableView layoutSubviews] + 178
8  UIKit                          0x26062a73 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
9  QuartzCore                     0x240fabcd -[CALayer layoutSublayers] + 128
10 QuartzCore                     0x240f6375 CA::Layer::layout_if_needed(CA::Transaction*) + 348
11 UIKit                          0x26078fe7 -[UIView(Hierarchy) layoutBelowIfNeeded] + 962
12 UIKit                          0x26124aed -[UINavigationController _layoutViewController:] + 1168
13 UIKit                          0x26122783 -[UINavigationController _layoutTopViewController] + 250
14 UIKit                          0x2613b5ff -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 634
15 UIKit                          0x2613b35b -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 370
16 UIKit                          0x2613b0a7 -[UINavigationTransitionView _cleanupTransition] + 758
17 UIKit                          0x260a1685 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 204
18 UIKit                          0x2609f9c5 +[UIViewAnimationState popAnimationState] + 316
19 UIKit                          0x2612e0b9 -[UINavigationTransitionView transition:fromView:toView:] + 1732
20 UIKit                          0x2612d9ef -[UINavigationTransitionView transition:toView:] + 26
21 UIKit                          0x261242bd -[UINavigationController _startTransition:fromViewController:toViewController:] + 2548
22 UIKit                          0x2612355f -[UINavigationController _startDeferredTransitionIfNeeded:] + 834
23 UIKit                          0x261231b5 -[UINavigationController __viewWillLayoutSubviews] + 52
24 UIKit                          0x2612312b -[UILayoutContainerView layoutSubviews] + 214
25 UIKit                          0x26062a73 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
26 QuartzCore                     0x240fabcd -[CALayer layoutSublayers] + 128
27 QuartzCore                     0x240f6375 CA::Layer::layout_if_needed(CA::Transaction*) + 348
28 QuartzCore                     0x240f6209 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
29 QuartzCore                     0x240f56d1 CA::Context::commit_transaction(CA::Transaction*) + 368
30 QuartzCore                     0x240f53a5 CA::Transaction::commit() + 520
31 QuartzCore                     0x240eeb2b CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
32 CoreFoundation                 0x21aad6c9 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
33 CoreFoundation                 0x21aab9cd __CFRunLoopDoObservers + 280
34 CoreFoundation                 0x21aabdff __CFRunLoopRun + 958
35 CoreFoundation                 0x219fb229 CFRunLoopRunSpecific + 520
36 CoreFoundation                 0x219fb015 CFRunLoopRunInMode + 108
37 GraphicsServices               0x22febac9 GSEventRunModal + 160
38 UIKit                          0x260cf189 UIApplicationMain + 144
39 AppName                        0xc57dc main (AppDelegate.swift:26)
40 (Missing)                      0x216a3873 (Missing)

0 个答案:

没有答案