从Photos.app获取最新图片?

时间:2012-01-15 04:17:39

标签: ios image camera photos

我见过其他应用程序,你可以从照片应用程序中导入最后一张照片以便快速使用,但据我所知,我只知道如何获取A图像而不是最后一张(最近一张)。谁能告诉我如何获取最后一张图片?

13 个答案:

答案 0 :(得分:106)

此代码段将从相机胶卷(iOS 7及以下版本)获取最新图像:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

            // Stop the enumerations
            *stop = YES; *innerStop = YES;

            // Do something interesting with the AV asset.
            [self sendTweet:latestPhoto];
        }
    }];
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
}];

iOS 8及以上版本:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
                                          targetSize:self.photoLibraryButton.bounds.size
                                         contentMode:PHImageContentModeAspectFill
                                             options:PHImageRequestOptionsVersionCurrent
                                       resultHandler:^(UIImage *result, NSDictionary *info) {

                                           dispatch_async(dispatch_get_main_queue(), ^{

                                               [[self photoLibraryButton] setImage:result forState:UIControlStateNormal];

                                           });
                                       }];

答案 1 :(得分:20)

iBrad的精彩回答,对我来说几乎完美无缺。唯一的例外是它以原始方向返回图像(例如倒置,-90°等)。

要解决此问题,我只需将fullResolutionImage更改为fullScreenImage

下面:

UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

它现在有效。

答案 2 :(得分:10)

iBrad的例子包括一个显然有效的iOS8片段,但我发现自己对他描述的返回类型感到困惑。这是一个抓取最后一个图像的片段,包括版本和大小要求的选项。

值得注意的是能够请求特定版本(原始,当前)和大小。在我的情况下,由于我希望将返回的图像应用于按钮,我请求它的大小和缩放以适合我正在应用它的按钮:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
                                          targetSize:self.photoLibraryButton.bounds.size
                                         contentMode:PHImageContentModeAspectFill
                                             options:PHImageRequestOptionsVersionCurrent
                                       resultHandler:^(UIImage *result, NSDictionary *info) {

                                           dispatch_async(dispatch_get_main_queue(), ^{

                                               [[self photoLibraryButton] setImage:result forState:UIControlStateNormal];

                                           });
                                       }];

答案 3 :(得分:8)

感谢您回答iBrad Apps。

当用户在他/她的照片卷上没有图像时,我想指出特殊情况下的错误预防(我知道奇怪的情况):

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    //Check that the group has more than one picture
    if ([group numberOfAssets] > 0) {
        // Chooses the photo at the last index
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets] - 1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

            // The end of the enumeration is signaled by asset == nil.
            if (alAsset) {
                ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

                [self.libraryButton setImage:latestPhoto forState:UIControlStateNormal];
            }
        }];
    }
    else {
      //Handle this special case
    }

答案 4 :(得分:6)

嗯,这是一个如何使用 Swift 3 来加载画廊中最后一张图片的解决方案:

func loadLastImageThumb(completion: @escaping (UIImage) -> ()) {
    let imgManager = PHImageManager.default()
    let fetchOptions = PHFetchOptions()
    fetchOptions.fetchLimit = 1
    fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]

    let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

    if let last = fetchResult.lastObject {
        let scale = UIScreen.main.scale
        let size = CGSize(width: 100 * scale, height: 100 * scale)
        let options = PHImageRequestOptions()


        imgManager.requestImage(for: last, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: options, resultHandler: { (image, _) in
            if let image = image {
                completion(image)
            }
        })
    }

}

如果您需要更快的速度,还可以使用PHImageRequestOptions并设置:

options.deliveryMode = .fastFormat
options.resizeMode = .fast

这是你在viewController中获取它的方式(你应该用你的类替换GalleryManager.manager):

GalleryManager.manager.loadLastImageThumb { [weak self] (image) in
      DispatchQueue.main.async {
           self?.galleryButton.setImage(image, for: .normal)
      }
}

答案 5 :(得分:5)

请参阅利亚姆的回答。 fullScreenImage将返回符合您设备屏幕尺寸的缩放图片。获取实际图像大小:

  ALAssetRepresentation *representation = [alAsset defaultRepresentation];
  ALAssetOrientation orientation = [representation orientation];
  UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:[representation scale] orientation:(UIImageOrientation)orientation];                    

fullResolutionImage上引用Apple的ALAssetRepresentation类参考:

  

要从CGImage创建正确旋转的UIImage对象,请使用   imageWithCGImage:scale:orientation:或   initWithCGImage:scale:orientation :,传递orientation的值   和规模。

答案 6 :(得分:4)

我发现了一个错字,我很尴尬地承认给我的时间超过了它应该弄清楚的时间。也许它会在一段时间内拯救别人。

此行在indexSetWithIndex之后缺少冒号:

[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets] - 1]options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

答案 7 :(得分:3)

这是 Swift 中的一个版本,它请求数据并将其转换为UIImage,因为提供的版本每次返回一个空的UIImage

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)

    if let lastAsset: PHAsset = fetchResult.lastObject as? PHAsset {
        let manager = PHImageManager.defaultManager()
        let imageRequestOptions = PHImageRequestOptions()

        manager.requestImageDataForAsset(lastAsset, options: imageRequestOptions) {
            (let imageData: NSData?, let dataUTI: String?,
             let orientation: UIImageOrientation,
             let info: [NSObject : AnyObject]?) -> Void in

             if let imageDataUnwrapped = imageData, lastImageRetrieved = UIImage(data: imageDataUnwrapped) {
                // do stuff with image

             }
        }
    }

答案 8 :(得分:2)

根据iBrad的回答,这里有一个快速的&在iOS 8.1中适用于我的脏Swift版本:

let imgManager = PHImageManager.defaultManager()
var fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
if let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {
    imgManager.requestImageForAsset(fetchResult.lastObject as PHAsset, targetSize: self.destinationImageView.frame.size, contentMode: PHImageContentMode.AspectFill, options: nil, resultHandler: { (image, _) in
        self.destinationImageView.image = image
    })
}

注意:这需要iOS 8.0+。请务必链接照片框架并在文件中添加“导入照片”。

答案 9 :(得分:1)

Heres是iBrad& amp;哈维尔的答案(效果很好),但我得到缩略图资产而不是全分辨率图像。其他一些人可能会觉得这很方便。

- (void)setCameraRollImage {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        if ([group numberOfAssets] > 0) {
            // Chooses the photo at the last index
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets] - 1)] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
                // The end of the enumeration is signaled by asset == nil.
                if (alAsset) {
                    UIImage *latestPhoto = [UIImage imageWithCGImage:[alAsset thumbnail]];
                    [self.cameraRollButton setImage:latestPhoto forState:UIControlStateNormal];
                }
            }];
        }
    } failureBlock: ^(NSError *error) {
    }];
}

答案 10 :(得分:0)

accepted answer (how to get last image)的Xamarin.iOS版本,包括其他答案的所有通知:

  private void ChooseLastTakenPictureImplementation()
    {
        var library = new ALAssetsLibrary();
        // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
        library.Enumerate(ALAssetsGroupType.SavedPhotos, (ALAssetsGroup assetsGroup, ref bool stop) =>
            {
                if (stop || assetsGroup == null)
                {
                    return;
                }
                //Xamarin does not support ref parameters in nested lamba expressions
                var lambdaStop = false;
                //Check that the group has more than one picture
                if (assetsGroup.Count > 0)
                {
                    // Within the group enumeration block, filter to enumerate just photos.
                    assetsGroup.SetAssetsFilter(ALAssetsFilter.AllPhotos);
                    // Chooses the photo at the last index
                    assetsGroup.Enumerate(NSEnumerationOptions.Reverse, (ALAsset result, int index, ref bool innerStop) =>
                        {
                            // The end of the enumeration is signaled by asset == nil.
                            if (result != null)
                            {
                                var representation = result.DefaultRepresentation;
                                var latestPhoto = new UIImage(representation.GetImage(), representation.Scale, (UIImageOrientation)representation.Orientation);
                                // Stop the enumerations
                                lambdaStop = innerStop = true;
                                // Do something interesting with the AV asset.
                                HandleImageAutoPick(latestPhoto);
                            }
                        });
                    stop = lambdaStop;
                    return;
                }
                else
                {
                    //Handle this special case where user has no pictures
                }
            }, error =>
            {
                // Typically you should handle an error more gracefully than this.
                Debug.WriteLine(error.Description);
            });
    }

答案 11 :(得分:0)

这是一个非常酷的方法,但其中一个问题是您必须能够在运行时实例化PHPhotoLibrary和其他PHPhoto类,否则iOS 7.XX上会出现链接错误只是想指出这一点,因为我现在遇到这些问题。

另外我相信你必须在Photos框架中使用弱链接才能让应用程序在安装了iOS 8.XX和iOS 7.XX的设备上运行(虽然我还没有测试过它。)

我遇到的问题是如何在运行时实例化PHPhotoLibrary。有人有代码片段吗?

实际上对于我正在处理的应用程序,我确实必须编写运行时代码来实例化PHPhotoLibrary类并调用PHotos框架方法,以便应用程序可以在iOS 7.x.x和iOS 8.x.x上运行。其他人可能遇到同样的问题所以我提供了以下代码 - >

// PHPhotoLibrary_class will only be non-nil on iOS 8.x.x
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");

if (PHPhotoLibrary_class) {

   /**
    *
    iOS 8..x. . code that has to be called dynamically at runtime and will not link on iOS 7.x.x ...

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating album: %@", error);
        }
    }];
    */

    // dynamic runtime code for code chunk listed above            
    id sharedPhotoLibrary = [PHPhotoLibrary_class performSelector:NSSelectorFromString(@"sharedPhotoLibrary")];

    SEL performChanges = NSSelectorFromString(@"performChanges:completionHandler:");

    NSMethodSignature *methodSig = [sharedPhotoLibrary methodSignatureForSelector:performChanges];

    NSInvocation* inv = [NSInvocation invocationWithMethodSignature:methodSig];
    [inv setTarget:sharedPhotoLibrary];
    [inv setSelector:performChanges];

    void(^firstBlock)() = ^void() {
        Class PHAssetCollectionChangeRequest_class = NSClassFromString(@"PHAssetCollectionChangeRequest");
        SEL creationRequestForAssetCollectionWithTitle = NSSelectorFromString(@"creationRequestForAssetCollectionWithTitle:");
        [PHAssetCollectionChangeRequest_class performSelector:creationRequestForAssetCollectionWithTitle withObject:albumName];

    };

    void (^secondBlock)(BOOL success, NSError *error) = ^void(BOOL success, NSError *error) {
       if (success) {
           [assetsLib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
               if (group) {
                   NSString *name = [group valueForProperty:ALAssetsGroupPropertyName];
                   if ([albumName isEqualToString:name]) {
                       groupFound = true;
                       handler(group, nil);
                   }
               }
           } failureBlock:^(NSError *error) {
               handler(nil, error);
           }];
       }

       if (error) {
           NSLog(@"Error creating album: %@", error);
           handler(nil, error);
       }
   };

   // Set the first and second blocks.
   [inv setArgument:&firstBlock atIndex:2];
   [inv setArgument:&secondBlock atIndex:3];

   [inv invoke];

}
else {   
   // code that always creates an album on iOS 7.x.x but fails
   // in certain situations such as if album has been deleted
   // previously on iOS 8...x. .              
   [assetsLib addAssetsGroupAlbumWithName:albumName
       resultBlock:^(ALAssetsGroup *group) {
       handler(group, nil);
   } failureBlock:^(NSError *error) {
       NSLog( @"Failed to create album: %@", albumName);
       handler(nil, error);
   }];
}

答案 12 :(得分:0)

以下代码适用于iOS7和iOS8。它还会检查过滤器中是否有图像。在执行代码之前,您应该检查相册权限:

// get the latest image from the album
-(void)getLatestPhoto
{
    NSLog(@"MMM TGCameraViewController - getLatestPhoto");
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        // Within the group enumeration block, filter to enumerate just photos.
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        // For this example, we're only interested in the last item [group numberOfAssets]-1 = last.
        if ([group numberOfAssets] > 0) {

            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1]
                                    options:0
                                 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                                     // The end of the enumeration is signaled by asset == nil.
                                     if (alAsset) {
                                         ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                         // Do something interesting with the AV asset.
                                         UIImage *img = [UIImage imageWithCGImage:[representation fullScreenImage]];

                                         // use the photo here ...


                                         // we only need the first (most recent) photo -- stop the enumeration
                                         *innerStop = YES;
                                     }
                                 }];
        }
    }
    failureBlock: ^(NSError *error) {
       // Typically you should handle an error more gracefully than this.
       NSLog(@"No groups");
    }];
}  

(此代码是here的修改版本。)