如何在Swift iOS8中删除最后一张照片? [英] How to delete the last photo in Swift iOS8?

查看:167
本文介绍了如何在Swift iOS8中删除最后一张照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从相机胶卷中获取最后一张照片并将其删除.现在我得到了最后一张照片,但是在删除最后一张照片时遇到了问题. 我尝试过这种方式,但是我删除了所有照片,因此我打算构建一个新的PHFetchResult,其中仅包含最后一张照片,但我不知道该怎么做.

I try to get the last photo from camera roll and delete it.Now I get the last photo but have problems in deleting the last photo. I tried this way but I delete all photos,so I plan to build a new PHFetchResult which only include the last photo but I don't know how to do that.

PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
      PHAssetChangeRequest.deleteAssets(fetchResult)},
      completionHandler: { 
      success, error in
      NSLog("Finished deleting asset. %@", (success ? "Success" : error))
      }) 

谢谢大家回答我的问题!

Thank all of you to answer my question!

推荐答案

我想问题出在您的fetchResult上. 您将必须传递一个仅包含照片库中最新图像的数组.

I guess the problem lies in your fetchResult. You will have to pass an array that contains only latest image from photo library.

尝试如下制作该数组-

var fetchOptions: PHFetchOptions = PHFetchOptions()

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

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

    if (fetchResult.lastObject != nil) {

        var lastAsset: PHAsset = fetchResult.lastObject as PHAsset

        let arrayToDelete = NSArray(object: lastAsset)

        PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
            PHAssetChangeRequest.deleteAssets(arrayToDelete)},
            completionHandler: {
                success, error in
                NSLog("Finished deleting asset. %@", (success ? "Success" : error))
        }) 



    }

请参见

这篇关于如何在Swift iOS8中删除最后一张照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆