ios 编程--使用代码创建新的相册随加项目

2013-04-23 13:29:04|  分类: 默认分类 |举报|字号 订阅
编写ios 编程--使用代码创建新的相册,是参考http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/来写的。

ALAssetsLibrary+CustomPhotoAlbum.h 里的代码是:
#import <Foundation/Foundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
typedef void(^SaveImageCompletion)(NSError* error);
@interface ALAssetsLibrary(CustomPhotoAlbum)
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;
@end


ALAssetsLibrary+CustomPhotoAlbum.m里的代码是:

#import "ALAssetsLibrary+CustomPhotoAlbum.h"

@implementation ALAssetsLibrary(CustomPhotoAlbum)

-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock

{
    [self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation 

                        completionBlock:^(NSURL* assetURL, NSError* error) {
                          if (error!=nil) {
                              completionBlock(error);
                              return;

                          }
                          [self addAssetURL: assetURL 
                                    toAlbum:albumName 
                        withCompletionBlock:completionBlock];
                      }];
}
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock{

    __block BOOL albumWasFound = NO;
    [self enumerateGroupsWithTypes:ALAssetsGroupAlbum 

    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                                albumWasFound = YES;
                                [self assetForURL: assetURL 
                                      resultBlock:^(ALAsset *asset) {
                                          [group addAsset: asset];
                                          completionBlock(nil);
                                      } failureBlock: completionBlock];
                                return;
                            }
                            if (group==nil && albumWasFound==NO) {
                                __weak ALAssetsLibrary* weakSelf = self;
                                [self addAssetsGroupAlbumWithName:albumName 
                                                      resultBlock:^(ALAssetsGroup *group) {
                                                          [weakSelf assetForURL: assetURL 
                                                                        resultBlock:^(ALAsset *asset) {
                                                                            [group addAsset: asset];
                                                                            completionBlock(nil);
                                                                        } failureBlock: completionBlock];
                                                      } failureBlock: completionBlock];
                                return;
                            }
                        } failureBlock: completionBlock];
}
@end 接下来,在AppDelegate.m导入RootViewController。
RootViewController.h 里的代码是:
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface RootViewController : UIViewController <UIImagePickerControllerDelegate>

@property (strong, atomic) ALAssetsLibrary * library;

@end

在RootViewController.m里的代码是:
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize library;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;


}



-(void)dealloc
{
    [library release];
    library = nil;
    [super dealloc];
}



- (void)viewDidLoad
{
    [super viewDidLoad];


    // Do any additional setup after loading the view.
    
    library = [[ALAssetsLibrary alloc] init];


    
    
    UIButton * createGroup = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    createGroup.frame = CGRectMake( 20, 150, 100, 40);
    [createGroup setTitle:@"takePhoto" forState:UIControlStateNormal];


    [createGroup addTarget:self action:@selector(takePhotoClick) forControlEvents:UIControlEventTouchUpInside];


    [self.view addSubview:createGroup];
    
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];


    // Dispose of any resources that can be recreated.
}



- (void)takePhotoClick
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];


    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;


    imagePickerController.editing = YES;
    imagePickerController.delegate = (id)self;
    
    [self presentModalViewController:imagePickerController animated:YES];


}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [library saveImage:image toAlbum:@"Touch Code Magazine" withCompletionBlock:^(NSError *error) {


        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];
    [picker dismissModalViewControllerAnimated:NO];


}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:NO];


}
@end



接下来,导入AssetsLibrary.framework。就可以运行了。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。