【问题标题】:How to get the most recent photo from the Camera Roll on iOS by using Photos framework?如何使用 Photos 框架从 iOS 上的相机胶卷中获取最新照片?
【发布时间】:2019-05-28 16:35:59
【问题描述】:

我正在 iOS 中开发一个框架。我的框架中有一个函数,它使用 Photos 框架从相机胶卷中获取最新的图像。此功能在大多数使用我的框架的项目中都能成功运行。但它只在这些项目中的一个项目上崩溃。我尝试了多种方法来修复此崩溃,但在此崩溃中没有任何改变。

我检查了这个项目设置,但我找不到与我的项目设置有任何区别。我还检查了 plist 文件中的画廊权限。还有一个有趣的点。该项目成功使用了我的库的几乎所有功能,但是当我的库尝试访问该项目中的 UIImagePickerController 时,它也会崩溃。

@interface Controller () 
{
    UIBarButtonItem *barButtonItem;
}

@property(nonatomic, strong) PHFetchResult *fetchResult;
@property(nonatomic, strong) PHAsset *lastAsset;

@end

// calling this in the main thread
- (void)checkGalleryPermission
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusAuthorized)
        {
            [self setLastGalleryImageToPhotoGallery];
        }
        else if (status == PHAuthorizationStatusNotDetermined)
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
                {
                    if (status == PHAuthorizationStatusAuthorized)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{

                            [self setLastGalleryImageToPhotoGallery];
                        });
                     }
                }];
        }
}

- (void)setLastGalleryImageToPhotoGallery
{
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    self.fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
    if (self.fetchResult != nil)
    {
        self.lastAsset = [self.fetchResult lastObject];
        if (self.lastAsset != nil)
        {
            PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
            imageRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
            imageRequestOptions.synchronous = YES; // tried NO
            imageRequestOptions.version = PHImageRequestOptionsVersionCurrent;
            [[PHImageManager defaultManager] requestImageForAsset:self.lastAsset targetSize:CGSizeMake(42.0f, 42.0f) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info)
             {
                 dispatch_async(dispatch_get_main_queue(), ^{

                     [self->barButtonItem setImage:[result imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
                 });
             }];
        }
    }
}

我将日志放在上述函数中以查找崩溃的位置。它在下面的代码之后崩溃。我无法弄清楚问题所在。项目设置是否有任何限制到达相机胶卷或者这个问题与线程有关?

self.fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];

【问题讨论】:

    标签: ios objective-c phasset photosframework


    【解决方案1】:

    如果在使用 UIImagePickerController 时也发生同样的崩溃,那么这台设备上的 iOS 照片库很可能存在问题。 iOS 照片库是一个核心数据数据库,可能会损坏。你有没有看崩溃报告。如果您发现任何 SQL-/Core-Data 问题,则表明存在数据库问题。

    【讨论】:

      猜你喜欢
      • 2011-11-26
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多