- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我似乎遇到了一个我不知道为什么会发生的随机问题。我似乎无法让观察者调用 photoLibraryDidChange:(PHChange *)changeInstance
。我做了几个空白项目,所有项目都在证明这个问题,更改观察者有时会在初始应用程序安装时被调用,但在我在照片应用程序中执行更改后永远不会被调用。我也重置了模拟器无济于事。如果能提供任何帮助,我将不胜感激。
代码:
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface ViewController : UIViewController <PHPhotoLibraryChangeObserver>
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
{
if (status == PHAuthorizationStatusAuthorized)
{
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
{
[self setup];
});
}
}];
}
- (void)setup
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];
fetchOptions.wantsIncrementalChangeDetails = YES;
PHFetchResult *smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
for (PHAssetCollection *sub in smartAlbumsFetchResult)
{
PHFetchResult *fetch = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];
}
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
NSLog(@"Not called");
}
- (void)dealloc
{
[PHPhotoLibrary.sharedPhotoLibrary unregisterChangeObserver:self];
}
最佳答案
我认为您的测试方式有问题。这对我来说可以。这是我所做的。
这是我的一个 View Controller 的全部代码:
#import <UIKit/UIKit.h>
@import Photos;
#import "ViewController.h"
@interface ViewController() <PHPhotoLibraryChangeObserver>
@end
@implementation ViewController : UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
}
}];
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
NSLog(@"Here");
}
@end
我在模拟器中运行该应用程序。请求授权。我授权。在运行 Xcode 的模拟器后面,我在控制台中看到了“这里”——这是预料之中的,因为当库在授权后“生效”时,我们会收到更改通知。这正是观察者应有的行为方式。
仍然在模拟器中,我按 Shift-Command-H 转到跳板。我切换到“照片”应用并删除了一张照片。
在模拟器中,我按了两次 Shift-Command-H 以进入应用程序切换器。
在模拟器中,我单击仍在运行的测试应用程序以返回到它。在 Xcode 的模拟器后面,我在控制台中看到“这里”,因为我们不在的时候,一张照片被删除了。同样,这正是观察者的行为方式。
关于ios - 未调用 PHPhotoLibrary 更改观察器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722590/
我一直在尝试寻找一种替代方法,以在 iOS 8 中使用 Photos API 获取相册名称。使用 ALAssets,我们可以使用:valueForProperty:ALAssetsGroupPrope
我正在为 Mac OS X (10.11) 开发照片扩展。 我导入了 Photos 和 PhotosUI 框架。 除了我尝试调用 PHPhotoLibrary.sharedPhotoLibrary()
我有一个 NSURLDownloadTask 可以在后台成功下载文件(大图像或视频文件)。我成功地复制了 url 并调用此函数将我的 url 保存到照片库。如您所见,我想发送一个 UILocalNot
随着 iOS 8 中的新功能,如果您在应用程序中使用相机,它会请求访问相机的权限,然后当您尝试重新拍摄照片时,它会请求访问照片库的权限。下次我启动应用程序时,我想检查相机和照片库是否有访问权限。 对于
我正在尝试将视频保存到我的照片库中。但有时我会得到一个错误:操作无法完成。 (PHPhotosErrorDomain 错误 -1。) 这是我的代码: PHPhotoLibrary.shared().p
我触发了图像的后台下载。它成功了 - 位置是实际图像的路径。我没有收到错误消息,但图像也没有显示在照片应用程序中。我已经设置了 NSPhotoLibraryUsageDescription Info.
要将图像导出到手机图库,我尝试使用 ALAssetsLibrary,但 Xcode 8.0 通知我该方法已弃用,我应该使用 PHPhotoLibrary。当我使用 ALAssetsLibrary 时,
我正在使用 PHPhotoLibrary 创建自定义文件夹并在其中添加图片。它工作正常,但我添加到自定义文件夹的图片也会添加到“相机胶卷”文件夹中。我找到了这个旧线程Add photos in cus
我正在尝试从 ALAssetsLibrary 转换为 PHPhotoLibrary,因为它已被弃用。我需要从照片库中选择的照片中获取日期,但无法找出正确的方法。这是我目前正在做的事情。 mediaUr
我正在检查相机胶卷的权限: -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:YES]; PHAuthorizati
在测试我的应用程序时,我希望能够重置 +[PHPhotoLibrary authorizationStatus] 以检查应用程序在库未获得授权时是否正常运行。 我已尝试删除并重新安装应用程序,但是,授
为了测试,我试图重新创建系统“请求访问”弹出窗口体验。 更新: iOS 11下,删除App后,会再次出现系统弹窗。 (上一个问题) App 第一次运行时(也是唯一 次),系统弹出窗口显示,请求访问。之
我曾经使用 ALAssetLibrary 的 writeImageToSavedPhotosAlbum:metadata:completionBlock 将照片保存到相机胶卷,但现在在 iOS 9.0
我在应用程序中具有将图片保存在照片库中的功能。我想知道如何测试这段代码: func saveInPhotoGallery() { guard self.cameraOutput != nil
我似乎遇到了一个我不知道为什么会发生的随机问题。我似乎无法让观察者调用 photoLibraryDidChange:(PHChange *)changeInstance。我做了几个空白项目,所有项目都
我想做的是将视频保存到 PHPhotoLibrary,然后在上传到应用程序中的客户端远程服务器完成时删除它们(基本上,照片库用作临时存储以添加额外的安全层以防万一失败(我已经将我的视频保存在应用程序目
我在两种情况下使用 UIImagePickerController 选择照片库中的现有图像 拍一张新照片 在第一种情况下,当我从库中选择图像时,我可以轻松地在委托(delegate)方法中获取 URL
大家好,我正在使用 ALAssetsLibrary 将视频/图像写入相册。但 XCode 告诉我 ALAssetsLibrary 已被弃用,我需要使用 PHPhotoLibrary,这一切都很好,但没
我正在使用以下代码将图像保存到我在 iOS 照片库中创建的自定义文件夹中: 我收到此错误:Error Domain=NSCocoaErrorDomain Code=-1 "(null)" 谁能帮我解决
对于 iOS9,ALAssetsLibrary 已弃用。那么如何将其更改为 PHPPhotoLibrary 而不是 ALAssets? if (RecordedSuccessfully && reco
我是一名优秀的程序员,十分优秀!