我已经实现了今天用于显示最近录制的视频的小部件。我正在使用小部件故事板文件中的集合视图。
一切正常。我能够从共享容器中获取所需的拇指并将其绑定到集合视图。现在的问题是每当我在通知中心查找小部件时,它每次都会调用ViewDidLoad
TodayWidgetViewController
。因为这个集合视图似乎每次都重新加载。
有没有办法防止每次都重新加载?
提前致谢。
以下是已实施的源代码:
{
@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)
}
}
}
答案 0 :(得分:0)
text/plain
和viewWillAppear
。
关于viewDidAppear
似乎很奇怪,你确定吗?无论如何这不应该发生,这是不正常的,尝试检查这个方法:
viewDidLoad
你的扩展程序可能有错误,每次出现时都会再次调用它。
希望它有所帮助。
更新:(更正后)
由于尺寸问题,似乎您的小部件崩溃或未加载。
在func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -> Void)) {
// you could call here a custom function to update your widget
completionHandler(NCUpdateResult.NewData)
}
尝试启动此方法:
viewWillAppear