简体   繁体   English

如何快速刷新图库?

[英]How to refresh photo gallery in swift?

How can I refresh the photo gallery in swift? 如何快速刷新图库? If I take a picture I have to close the app and then restart it to see it. 如果我拍照,则必须关闭该应用程序,然后重新启动以查看它。 I want to refresh the gallery automatically. 我想自动刷新图库。 What's the best way to solve this problem? 解决此问题的最佳方法是什么?

It depends on how you implement that functionality. 这取决于您如何实现该功能。

1) If you use Tableview/ColloectionView, 1)如果您使用Tableview / ColloectionView,

After taking picture Option click Use in opened CameraViewController.You have to implement UIImagePickerControllerDelegate, UINavigationControllerDelegate.Delegate method : 拍照选项后,在打开的CameraViewController中单击“ 使用” 。您必须实现UIImagePickerControllerDelegate,UINavigationControllerDelegate.Delegate方法:

    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
   {
          //Made your code according your requirements.
         //Reload TableView/ColloectionView..
   }

2) If you show in just UIImageView, 2)如果仅在UIImageView中显示,

- (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
   {
          //Made your code according your requirements.
          [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
    //show image in your imageview..
   }

Add this to your viewDidLoad method: 将此添加到您的viewDidLoad方法:
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.refreshView), userInfo: nil, repeats: true) and implement in the refreshView what you want to do. NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.refreshView), userInfo: nil, repeats: true)然后在refreshView中实现您想要的操作。

Better method: use an observer pattern ! 更好的方法:使用observer pattern

After few investigation I found that, whenever You are taking a picture from device's camera your app will go in background and then you will bring your app to foreground. 经过几次调查,我发现,每当您从设备的相机拍摄照片时,您的应用程序都会在后台运行,然后将您的应用程序置于前台。 So, in my opinion you have to keep an instance of the tableView/CollectionView(depends upon your implementation) in AppDelegate. 因此,我认为您必须在AppDelegate中保留tableView / CollectionView的实例(取决于您的实现)。 Reload your tableView/CollectionView as 将您的tableView / CollectionView重新加载为

func applicationWillEnterForeground(application: UIApplication) { self.yourTableView.reloadData() }

in your AppDelegate.swift applicationWillEnterForeground function. 在您的AppDelegate.swift applicationWillEnterForeground函数中。 I think this will solve your problem. 我认为这可以解决您的问题。

Or, Simply make a delegate and call it in your AppDelegate.swift applicationWillEnterForeground function, and implement the delegate function in your ViewController and reload the tableView or collectionView. 或者,只需创建一个委托 ,然后在您的AppDelegate.swift applicationWillEnterForeground函数中调用它,然后在ViewController中实现该委托函数,然后重新加载tableView或collectionView。 Both solutions are working for me. 两种解决方案都对我有用。 I think it will also work for you. 我认为它也会为您服务。

If you are using PHPhotoLibrary This Link might be extremely helpful for you. 如果您正在使用PHPhotoLibrary,则此链接可能对您非常有帮助。 You just need to register a change observer and start observing, it's very well explained in the link above. 您只需要注册一个变更观察者并开始观察,就可以在上面的链接中很好地解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM