将照片保存到iPhone照片库中的自定义相册中
我正尝试在iPhone的照片库中创建一个自定义相册,然后保存我用相机拍摄的照片或从相机胶卷手机中选择的照片保存到该自定义相册中。 我可以成功创建相册,但照片不会保存在那里,而是保存到模拟器中保存的照片专辑...我不确定如何告诉UIImageWriteToSavedPhotosAlbum
保存到我刚刚创建的新相册中addAssetsGroupAlbumWithName
...
这里是我迄今为止的代码 - 我已经剪掉了一些部分来保持我的代码示例简短。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage])
{
// pull GPS information from photos metadata using ALAssetsLibrary
void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
{
// code snipped out
};
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL
resultBlock:ALAssetsLibraryAssetForURLResultBlock
failureBlock:^(NSError *error)
{
// code snipped out
}];
// getimage from imagePicker and resize it to the max size of the iPhone screen
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *resizedImage = [util_ createThumbnailForImage:originalImage thumbnailSize:[util_ determineIPhoneScreenSize]];
NSData *imageData = UIImagePNGRepresentation(resizedImage);
// code snipped out
// code snipped out
// code snipped out
// code snipped out
// code snipped out
// code snipped out
// create a new album called "My Apps Photos"
[library addAssetsGroupAlbumWithName:@"My Apps Photos"
resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"in addAssetsGroupAlbumWithName resultBlock");
// save file to album
UIImageWriteToSavedPhotosAlbum(resizedImage, self, nil, nil);
}
failureBlock:^(NSError *error)
{
NSLog(@"in addAssetsGroupAlbumWithName failureBlock");
}
];
}
}
所以...就像我说的那样,它会创建新专辑,但不会保存照片。 我该如何将它保存到新专辑中? 也许我的声音不使用UIImageWriteToSavedPhotosAlbum
?
注意:我使用Xcode 4.3.2,IOS 5.1和ARC
如果您使用的是iOS6,Fernando的答案将无法使用,因为saveImage选择器不再可用。
这个过程非常混乱,我还没有看到任何明确的答案,所以这里是我用来在iOS6中解决这个问题的方法。
您将需要使用以下组合:
创建相册:
[self.library addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
NSLog(@"added album:%@", albumName);
}
failureBlock:^(NSError *error) {
NSLog(@"error adding album");
}];
找到相册:
__block ALAssetsGroup* groupToAddTo;
[self.library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
NSLog(@"found album %@", albumName);
groupToAddTo = group;
}
}
failureBlock:^(NSError* error) {
NSLog(@"failed to enumerate albums:nError: %@", [error localizedDescription]);
}];
将图像保存到素材资源库,并将其放入相册中:
CGImageRef img = [image CGImage];
[self.library writeImageToSavedPhotosAlbum:img
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL* assetURL, NSError* error) {
if (error.code == 0) {
NSLog(@"saved image completed:nurl: %@", assetURL);
// try to get the asset
[self.library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// assign the photo to the album
[groupToAddTo addAsset:asset];
NSLog(@"Added %@ to %@", [[asset defaultRepresentation] filename], albumName);
}
failureBlock:^(NSError* error) {
NSLog(@"failed to retrieve image asset:nError: %@ ", [error localizedDescription]);
}];
}
else {
NSLog(@"saved image failed.nerror code %in%@", error.code, [error localizedDescription]);
}
}];
费尔南多的答案在iOS 7中为我工作。
脚步 :
1)从这里下载ALAssetsLibrary + CustomPhotoAlbum代码:http://www.touch-code-magazine.com/wp-content/uploads/2011/11/ALAssetsLibrary_CustomPhotoAlbum.zip?a071b6并复制该文件中的2个文件Xcode项目。
2)在你的头文件中,添加以下几行
#import <AssetsLibrary/AssetsLibrary.h>
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
@property (strong, atomic) ALAssetsLibrary* library;
3)在您的实现文件中,添加以下行
@synthesize library=_library;
编辑 :4)初始化资产库实例,最好在“viewDidLoad”方法,否则下面的saveImage方法将不会执行。 ( 确认 ):
[我正在重新提交建议编辑,因为有非iPhone技能的人拒绝了以前的提交。 这无疑是@santhu&Fernando的一个很好的答案,并且帮助了我很多,然而,初始化的一段代码丢失了,所以花了我一些时间来弄清代码为什么不起作用。 因此,如果有iPhone开发技能的版主审核编辑,我将不胜感激。]
_library = [[ALAssetsLibrary alloc] init];
5)在您想要保存的方法中添加此项
//Add this in the method where you wish to save
[self.library saveImage:(UIImage *) toAlbum:(NSString *) withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Big error: %@", [error description]);
}
}];
对于迅速用户: - 我做了同样的事情。
声明闭包超大类定义(高于类定义)
typealias CompletionHandler = (success:Bool!) -> Void
在类中声明库变量
var library:ALAssetsLibrary?;
初始化viewDidLoad中的变量
library = ALAssetsLibrary();
方法将图像添加到特定专辑
func addImage(image:UIImage, metaData:NSDictionary, toAlbum albumName:String, handler:CompletionHandler){
library?.addAssetsGroupAlbumWithName(albumName, resultBlock: {(group:ALAssetsGroup!) -> Void in
print("nAlbum Created:= (albumName)");
/*-- Find Group --*/
var groupToAddTo:ALAssetsGroup?;
self.library?.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
usingBlock: { (group:ALAssetsGroup?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
if(group != nil){
if group!.valueForProperty(ALAssetsGroupPropertyName) as String == albumName{
groupToAddTo = group;
print("nGroup Found (group!.valueForProperty(ALAssetsGroupPropertyName))n");
self.library?.writeImageToSavedPhotosAlbum(image.CGImage, metadata:metaData, completionBlock: {(assetURL:NSURL!,error:NSError!) -> Void in
if(error == nil){
self.library?.assetForURL(assetURL,
resultBlock: { (asset:ALAsset!) -> Void in
var yes:Bool? = groupToAddTo?.addAsset(asset);
if (yes == true){
handler(success: true);
}
},
failureBlock: { (error2:NSError!) -> Void in
print("Failed to add asset");
handler(success: false);
});
}
});
}
} /*Group Is Not nil*/
},
failureBlock: { (error:NSError!) -> Void in
print("Failed to find group");
handler(success: false);
});
}, failureBlock: { (error:NSError!) -> Void in
print("Failed to create (error)");
handler(success: false);
});
}
调用此方法为: -
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
var image:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage;
var metadata:NSDictionary = info[UIImagePickerControllerMediaMetadata] as NSDictionary;
self.addImage(image, metaData: metadata, toAlbum: "SwiftAlbum") { (success) -> Void in
print("Image Added : (success)");
}
picker.dismissViewControllerAnimated(true, completion: nil);
}
链接地址: http://www.djcxy.com/p/66523.html