如何在swift中刷新照片库?如果我拍照,我必须关闭应用程序然后重新启动才能看到它。我想自动刷新图库。解决这个问题的最佳方法是什么?
答案 0 :(得分:0)
这取决于您如何实现该功能。
1)如果您使用Tableview / ColloectionView,
拍照后选择在打开的CameraViewController中点击使用。你必须实现UIImagePickerControllerDelegate,UINavigationControllerDelegate.Delegate方法:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Made your code according your requirements.
//Reload TableView/ColloectionView..
}
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..
}
答案 1 :(得分:0)
将此添加到viewDidLoad方法:
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.refreshView), userInfo: nil, repeats: true)
并在refreshView中实现您想要做的事情。
更好的方法:
使用observer pattern
!
答案 2 :(得分:0)
经过一番调查后我发现,无论何时你从设备的相机拍照,你的应用程序都会进入后台,然后你就可以将你的应用程序带到前台了。因此,在我看来,你必须在AppDelegate中保留tableView / CollectionView的实例(取决于你的实现)。 将tableView / CollectionView重新加载为
func applicationWillEnterForeground(application: UIApplication) {
self.yourTableView.reloadData()
}
在您的AppDelegate.swift applicationWillEnterForeground 函数中。我认为这将解决您的问题。
或者, 只需创建 委托 并在AppDelegate.swift applicationWillEnterForeground 函数中调用它,并在ViewController中实现委托函数并重新加载tableView或collectionView 。 这两种解决方案都适合我。我认为它也适合你。
如果您使用PHPhotoLibrary This Link可能对您非常有帮助。您只需要注册一个变更观察者并开始观察,它在上面的链接中得到了很好的解释。