我正在构建一个带有按钮的应用程序,可以进行屏幕截图现在我想在同一自定义名称应用专辑中保存在此应用中制作的所有屏幕截图。我已经知道如何在应用程序首次打开时创建相册,代码如下。 我正在使用Photos框架创建我的专辑,如:
-(void)createAppAlbum
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"App Album Name"];
albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
}
completionHandler:^(BOOL success, NSError *error) {
if (success) {
fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
assetCollection = fetchResult.firstObject;
}
else {
NSLog(@"Error creating album: %@", error);}
}];
}
所以我的应用程序创建名为" App Album Name"的专辑。 我可以使用以下按钮将我的截图存储在我的新相册中:
-(IBAction)screenshot:(id)sender
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error creating asset: %@", error);
}
}];
}
如果我离开这个viewController,稍后再回来,我想找到我已创建的专辑,并在同一专辑中保存另一个屏幕截图,因为我不想每次创建新专辑进入此视图控制器。
所以我的问题是,我如何获得第一次通过Name创建的专辑,然后保存新的屏幕截图。
答案 0 :(得分:0)
由于此问题专门针对iOS 8,因此此代码可以正常使用:
- (void)saveImage:(UIImage *)image {
if (!self.library) {
self.library = [[ALAssetsLibrary alloc] init];
}
__weak ALAssetsLibrary *lib = self.library;
[self.library addAssetsGroupAlbumWithName:@"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
///checks if group previously created
if(group == nil){
//enumerate albums
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
//if the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {
//save image
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
//then get the image asseturl
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
//put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
}failureBlock:^(NSError *error){
}];
}else{
// save image directly to library
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error) {
}];
}
为了测试这是否有效,我创建了一个单一的屏幕应用程序,在视图中添加了一个按钮,并添加了一个截取屏幕截图然后将图像传递给saveImage:
函数的方法。这是我添加到它的代码,以使其工作:
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()
@property (nonatomic, strong) UIButton *screenshotButton;
@property (nonatomic, strong) ALAssetsLibrary *library;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.screenshotButton];
}
- (UIButton *)screenshotButton {
if (!_screenshotButton) {
_screenshotButton = [[UIButton alloc] initWithFrame:CGRectInset(self.view.bounds, 100.0f, 120.0f)];
_screenshotButton.layer.borderColor = [UIColor blueColor].CGColor;
_screenshotButton.layer.borderWidth = 2.0f;
_screenshotButton.layer.cornerRadius = 5.0f;
[_screenshotButton setTitle:@"Take Screenshot" forState:UIControlStateNormal];
[_screenshotButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_screenshotButton addTarget:self action:@selector(takeScreenshot) forControlEvents:UIControlEventTouchUpInside];
}
return _screenshotButton;
}
- (void)takeScreenshot {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imgData = UIImagePNGRepresentation(image);
if(imgData)
[imgData writeToFile:@"screenshot.png" atomically:YES];
else
NSLog(@"error while taking screenshot");
UIColor *randomColor = [UIColor colorWithHue:((CGFloat)(arc4random()%255)/255.0f)
saturation:((CGFloat)(arc4random()%255)/200.0f)
brightness:((CGFloat)(arc4random()%100)/255.0f) + 0.5f
alpha:1.0f];
self.screenshotButton.layer.borderColor = randomColor.CGColor;
[self.screenshotButton setTitleColor:randomColor forState:UIControlStateNormal];
[self saveImage:image];
}
有几点需要注意:
AssetsLibrary
,因此不建议这样做
在iOS 8之后使用,但仍然可以使用。 UIImage
的任何saveImage:
一起使用,因此它甚至可以正常工作
如果你正在使用另一个图书馆#import <AssetsLibrary/AssetsLibrary.h>
。