swift 将图片保存到自定义相册中

import UIKit
import Photos
@available(iOS 8.0, *)
class CustomAblum: NSObject {
    static let sharedInstance = CustomAblum()

    var assetCollection: PHAssetCollection!
    var albumFound : Bool = false
    var photosAsset: PHFetchResult!
    var collection: PHAssetCollection!
    var assetCollectionPlaceholder: PHObjectPlaceholder!
    static let albumName = "扫描图片"
    override init() {
        super.init();
        self.createAlbum()
    }
    private func createAlbum() {
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@",CustomAblum.albumName);
        let collection : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)

        if let _: AnyObject = collection.firstObject {
            self.albumFound = true
            assetCollection = collection.firstObject as! PHAssetCollection
        } else {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(CustomAblum.albumName);
                self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
                }, completionHandler: { success, error in
                    self.albumFound = (success ? true: false)

                    if (success) {
                        let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([self.assetCollectionPlaceholder.localIdentifier], options: nil)
                        print(collectionFetchResult)
                        self.assetCollection = collectionFetchResult.firstObject as! PHAssetCollection
                    }
            })
        }
    }
    func saveImage(image: UIImage) {
        if self.assetCollection != nil {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
                let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset
                let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
                albumChangeRequest?.addAssets([assetPlaceholder!])
                }, completionHandler: nil)
        }
    }

}

使用时直接掉saveImage方法。

时间: 2024-11-12 18:53:05

swift 将图片保存到自定义相册中的相关文章

android将应用中图片保存到系统相册并显示

我应用到的场景是程序中在视频通讯时截图,将截图保存到本地相册中 /*** @param bmp 获取的bitmap数据 * @param picName 自定义的图片名*/ public static void saveBmp2Gallery(Bitmap bmp, String picName) { String fileName = null; //系统相册目录 String galleryPath= Environment.getExternalStorageDirectory() + F

iOS--将图片保存至本地相册

今天做聊天,领导说对方给我发一个图片,我要保存到本地,IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAlbum方法对图像进行保存,该方法会将image保存至用户的相册中: 上代码: void UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo ); 参数说明: image : 需

前端如何保存页面图片,并在相册中查看?

第一次接触混合开发,然后碰到了很多H5+api,调用手机相册,扫码,保存图片等.做技术的注定了要终生学习,但学无止境,我们不可能把所有的知识技能都学完了,学精通了再去工作,我们需要在工作中去提升自己,在工作红去总结去成长. 我项目是用mui加H5+去开发的,所以在调用H5+的api时要等plusready事件发生后才能正常使用. mui.plusReady(function(){ //在这里面写你所调用的api plus.downloader.createDownload(url, option

iOS 开发: 自定义相册, 保存多张图片到自定义相册中

1.自定义相册(兼容 iOS7 iOS8) - (void)viewDidLoad { //search all photo albums in the library    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)     {         //compare the names of the albums        

将图片保存到系统相册的两种方法

第一种:采用系统的api直接使用: ContentResolver cr = getContentResolver(); String url = MediaStore.Images.Media.insertImage(cr, bmp, String.valueOf(System.currentTimeMillis()), ""); 但是,这种方式必须得刷新图库: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.par

iOS开发-iOS调用相机调用相册【将图片保存到本地相册】

设置头部代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> 1.调用相机 检测前置摄像头是否可用 - (BOOL)isFrontCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; } 检测后置摄像头是否可用 - (BOOL)isRe

iOS 图片保存到本地相册

点击按钮,将self.imageView上面的image内容保存到本地相册,并指定判断保存成功与否的方法imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo: - (IBAction)saveImageToAlbum:(id)sender {    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(imageSavedToPhotosAlbu

图片保存到系统相册

目前,UIImage保存到系统相册主要有两种方式: 方式一:(就一句话) UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError:contextInfo:),nil); 然后实现回调: - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf

照片保存到相册,并添加到自定义相册中

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { //相册存在标示 __block BOOL albumWasFound = NO; ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; //search all photo