最近公司的项目需要用到,将网络加载的URL写进二维码,并将二维码保存至本地相册。
还有就是,能把每次保存的图片替换成最新的(覆盖之前的图片)。(暂时不知道怎么实现)
以下是网络上找到的一些方法(供参考和本人记录)
获取图片
/**
* 获得刚才添加到【相机胶卷】中的图片
*/
- (PHFetchResult<PHAsset *> *)createdAssets
{
__block NSString *createdAssetId = nil;
// 添加图片到【相机胶卷】
// localIdentifier: 保存的图片的唯一标识,可以通过该标识获取对应图片
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdAssetId = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageNamed:@"想要保存的图片"]].placeholderForCreatedAsset.localIdentifier;
} error:nil];
if (createdAssetId == nil) return nil;
// 在保存完毕后取出图片
return [PHAsset fetchAssetsWithLocalIdentifiers:@[createdAssetId] options:nil];
}
自定义相册
/**
* 获得【自定义相册】
*/
- (PHAssetCollection *)createdCollection
{
// 获取软件的名字作为相册的标题
NSString *title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
// 获得所有的自定义相册
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumRegular
options:nil];
for (PHAssetCollection *collection in collections) {
if ([collection.localizedTitle isEqualToString:title]) {
return collection;
}
}
// 代码执行到这里,说明还没有自定义相册
__block NSString *createdCollectionId = nil;
// 创建一个新的相册
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:nil];
if (createdCollectionId == nil) return nil;
// 创建完毕后再取出相册
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionId] options:nil].firstObject;
}
保存图片到相册
/**
* 保存图片到相册
*/
- (void)saveImageIntoAlbum
{
// 获得相片
PHFetchResult<PHAsset *> *createdAssets = self.createdAssets;
// 获得相册
PHAssetCollection *createdCollection = self.createdCollection;
if (createdAssets == nil || createdCollection == nil) {
LRLog(@"保存失败!--1");
return;
}
// 将相片添加到相册
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
[request insertAssets:createdAssets atIndexes:[NSIndexSet indexSetWithIndex:0]];
} error:&error];
// 保存结果
if (error) {
LRLog(@"保存失败!--2");
} else {
LRLog(@"保存成功!--1");
}
}
保存
- (IBAction)save {
/*
requestAuthorization方法的功能
1.如果用户还没有做过选择,这个方法就会弹框让用户做出选择
1> 用户做出选择以后才会回调block
2.如果用户之前已经做过选择,这个方法就不会再弹框,直接回调block,传递现在的授权状态给block
*/
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
case PHAuthorizationStatusAuthorized: {
// 保存图片到相册
[self saveImageIntoAlbum];
break;
}
case PHAuthorizationStatusDenied: {
LRLog(@"提醒用户打开相册的访问开关");
if (oldStatus == PHAuthorizationStatusNotDetermined) return;
break;
}
case PHAuthorizationStatusRestricted: {
LRLog(@"因系统原因,无法访问相册!");
break;
}
default:
break;
}
});
}];
}
删除最后一张图片
- (IBAction)delete:(id)sender {
// 获取软件的名字作为相册的标题
NSString *title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
// 获取相册集合
PHFetchResult *collectonResuts = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:[PHFetchOptions new]] ;
// 对相册获取到的相册集合进行遍历
[collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PHAssetCollection *assetCollection = obj;
// 找到对应(软件名字)的相册
if ([assetCollection.localizedTitle isEqualToString:title]) {
// 获取对应(软件名字)相册中所有的照片
PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:[PHFetchOptions new]];
// 遍历对应(软件名字)相册中的所有照片
[assetResult enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 获取相册的最后一张照片
if (idx == [assetResult count] - 1) {
// 删除最后一张照片
[PHAssetChangeRequest deleteAssets:@[obj]];
}
} completionHandler:^(BOOL success, NSError *error) {
LRLog(@"Error: %@", error);
}];
}];
}
}];
}