2016-09-21 1 views
12

私の最初の質問はここです:)最近、私はXcodeを8に更新し、resizableSnapshotViewメソッドはいくつかのシミュレータで正しく動作しません。 snapshotViewはiOS9/10搭載のすべてのテストデバイスとiPhone6sのシミュレータでうまく動作しますが、iPhone7/7pシミュレータでは空です。私は7/7pがスナップショットにアクセスするためにいくつかの権限を必要とするかもしれないと思うが、私は彼らが何であるか分からない。iPhone 7/7plusの空のsnapshotView

let cell = self.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! CalendarCell  
var topFrame = cell.frame 
topFrame.origin.y = tableView.contentOffset.y 
topFrame.size.height -= tableView.contentOffset.y 
topSnapshotView = tableView.resizableSnapshotView(from: topFrame, afterScreenUpdates: false, withCapInsets: UIEdgeInsets.zero) 
+2

私は同じことが起こっている、それはシミュレータでのみ起こる聞くのは良いです。 – Alf

+0

まったく同じ問題ですが、実際のデバイスでこれをテストしていません。 – KTZ

+0

To Alf。残念ながら、私は実際のデバイスを持っていないと私はこれが実際のデバイスで発生するかどうか不明です。 – LYM

答えて

7

次のUIView拡張機能を使用して、CoreGraphicsを使用してスナップショットを作成します。

これはiPhone 7シミュレータで確認できます。

public extension UIView { 

    public func snapshotImage() -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0) 
     drawHierarchy(in: bounds, afterScreenUpdates: false) 
     let snapshotImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return snapshotImage 
    } 

    public func snapshotView() -> UIView? { 
     if let snapshotImage = snapshotImage() { 
      return UIImageView(image: snapshotImage) 
     } else { 
      return nil 
     } 
    } 
} 
+0

ありがとう、それは動作します。しかし、私の主な問題は 'resizableSnapshotView'を使うことです。私はあなたのコードでそれを書き換えるいくつかの時間を試みたが失敗した。フレームでsnapshotViewをクリップする方法を教えてください。 – LYM

+2

これは機能しません。それは古い方法と同じ結果を私にもたらします。 Xcode 8とiPhone 7/7 +シミュレータで試してみました。 – yuzer

+0

これはなぜ起こっているのですか? –

0

で動作:

  • バージョン8.1(8B62)
  • シミュレータバージョン10.0(SimulatorApp-700.14

スウィフト3

import Foundation 
import UIKit 

extension UIView { 
    func snapshotView() -> UIView? { 
     guard let image = snapshotImage() else { return nil } 
     return UIImageView(image: image) 
    } 

    func snapshotImage() -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, contentScaleFactor) 
     defer { UIGraphicsEndImageContext() } 

     drawHierarchy(in: bounds, afterScreenUpdates: false) 

     return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 
関連する問題