0
我已經實現了今天的Widget,用於顯示最近錄製的視頻thumb.i使用Widget Storyboard文件中的集合視圖。 它的一切工作正常。我能夠從共享容器中獲取所需的大拇指,並將其綁定到集合視圖。現在的問題是每當我在Notification中心尋找小部件時,它都會調用ViewDidLoad
的TodayWidgetViewController
。由於這個集合視圖似乎每次都會重新加載。ViewDid今天的Widget加載每次都會被調用
有沒有辦法防止每次重新加載?
在此先感謝。
下面是源代碼來實現:
{
@IBOutlet weak var widgetCollection: UICollectionView!
@IBOutlet weak var recordButton: UIButton!
var mutArryListOfVideosImg: NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.
self.recordButton.layer.borderWidth = 2.0
self.recordButton.layer.borderColor = UIColor.white.cgColor
self.recordButton.layer.masksToBounds = true
self.recordButton.layer.cornerRadius = self.recordButton.frame.size.width/2
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
//print("array count:\()")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.getVideoThumbImages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnRecordAction(sender:UIButton)
{
let pjUrl = URL(string: "")
self.extensionContext?.open(pjUrl!, completionHandler: { (Bool) in
})
}
func getDataPath() -> URL
{
let appGroupId = 「」
let appGroupDirectoryPath:URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!
let newDirectory = appGroupDirectoryPath.appendingPathComponent("VideoThumbs")
return newDirectory
}
func getVideoThumbImages()
{
let videoDirectoryPath = self.getDataPath()
if let Images = try? FileManager.default.contentsOfDirectory(atPath: videoDirectoryPath.path) as [String]
{
if((Images.count) > 0)
{
let sortedImages = (Images.sorted(by: backward)) as [String]
//print("Sorted Array:\(sortedImages)")
if((sortedImages.count) > 0)
{
for (index,element) in (sortedImages.enumerated()) {
print("\(index) = \(element)")
mutArryListOfVideosImg.add(element)
}
if(mutArryListOfVideosImg.count > 0)
{
widgetCollection.reloadData()
}
}
}
}
}
func backward(_ s1: String, _ s2: String) -> Bool {
return s1 > s2
}
func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) {
URLSession.shared.dataTask(with: url) {
(data, response, error) in
completion(data, response, error)
}.resume()
}
//MARK: CollectionView Delegate and Datasource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if mutArryListOfVideosImg.count > 3
{
return 3
}
else
{
return mutArryListOfVideosImg.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WidgetCollectionCell", for: indexPath as IndexPath) as! WidgetCollectionCell
cell.imgVideoThumbImage.layer.cornerRadius = 8.0
cell.imgVideoThumbImage.layer.masksToBounds = true
let directoryPath = self.getDataPath()
let url = directoryPath.appendingPathComponent(mutArryListOfVideosImg.object(at: indexPath.item) as! String)
getDataFromUrl(url: url) { (data, response, error) in
guard let data = data, error == nil else { return }
//print(response?.suggestedFilename ?? url.lastPathComponent)
//print("Download Finished")
DispatchQueue.main.async() {() -> Void in
cell.imgVideoThumbImage.image = UIImage(data: data)
}
}
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
print("Widget thumb tapped....")
let pjUrl = URL(string: 「CallBack://edit-\(mutArryListOfVideosImg.object(at: indexPath.item))")
self.extensionContext?.open(pjUrl!, completionHandler: { (Bool) in
})
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: (self.widgetCollection.frame.size.width/3) - 10, height: 70)
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.newData)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if (activeDisplayMode == NCWidgetDisplayMode.compact) {
self.preferredContentSize = maxSize
}
else {
self.preferredContentSize = CGSize(width: maxSize.width, height: 120)
}
}
}
甚至我不知道爲什麼viewDidLoad中獲取調用每次。這對我來說是個問題。我也實現了上述方法。 – hpp
在我的代碼下閱讀我的短語,肯定有一個錯誤,強制重新加載你的小部件,你必須找到它。您的問題中沒有編寫代碼,因此我不禁要糾正它,但您必須努力尋找此錯誤:我認爲這是好方法。 –
我已添加代碼。你可以檢查並讓我知道它有什麼問題嗎? – hpp