• 代码中怎么新建一个相簿?


     

    转载自:http://www.cocoachina.com/bbs/read.php?tid=143383

    我的创建相册代码:

    - (void)saveToAlbumWithMetadata:(NSDictionary*)metadata
                          imageData:(NSData*)imageData
                    customAlbumName:(NSString*)customAlbumName
                    completionBlock:(void(^)(void))completionBlock
                       failureBlock:(void(^)(NSError*error))failureBlock
    {
        ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    //接下来是疯狂的 block
    void(^AddAsset)(ALAssetsLibrary *, NSURL*) = ^(ALAssetsLibrary *assetsLibrary, NSURL*assetURL) {
    //1 遍历 AssetsLibrary 整个相册库 [assetsLibrary assetForURL:assetURL resultBlock:
    ^(ALAsset *asset) {
           //2 遍历 ALAssetsGroup 相册库中文件夹 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:
    ^(ALAssetsGroup *group, BOOL*stop) {
    //3 找到 customAlbumName 对应的相册库文件夹
    if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) { [group addAsset:asset]; if(completionBlock) { completionBlock(); } } } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; };

    //把照片写入相册 [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:
    ^(NSURL*assetURL, NSError*error) { if(customAlbumName) { [assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) { if(group) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [group addAsset:asset]; if(completionBlock) { completionBlock(); } } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; }else{ AddAsset(assetsLibrary, assetURL); } } failureBlock:^(NSError*error) { AddAsset(assetsLibrary, assetURL); }]; }else{ if(completionBlock) { completionBlock(); } } }]; } - (void)saveImage:(UIImage*)image{ ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray*groups=[[NSMutableArrayalloc]init]; ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL*stop) { if(group) { [groups addObject:group]; } else { BOOLhaveHDRGroup = NO; for(ALAssetsGroup *gp in groups) { NSString*name =[gp valueForProperty:ALAssetsGroupPropertyName]; if([name isEqualToString:@"Flickr墙纸"]) { haveHDRGroup = YES; } } if(!haveHDRGroup) { //do add a group named "HDR" [assetsLibrary addAssetsGroupAlbumWithName:@"Flickr墙纸" resultBlock:^(ALAssetsGroup *group) { [groups addObject:group]; } failureBlock:nil]; haveHDRGroup = YES; } } }; //创建相簿 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];

    //在我的"扫一扫"代码中,生成的二维码图片存到新创建的相薄后显示全黑,后同事指点原来是下边红色方法的问题,后换了个方法实现了: UIImageJPEGRepresentation(image,1) [self saveToAlbumWithMetadata:nilimageData:UIImagePNGRepresentation(image) customAlbumName:@"Flickr墙纸"completionBlock:^ { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存成功"message:@"若想马上启用该墙纸,请进入图库找到该墙纸并设置为墙纸即可,感谢您的使用。"delegate:nilcancelButtonTitle:@""otherButtonTitles: nil]; [alert show]; } failureBlock:^(NSError*error) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存失败"message:error.description delegate:nilcancelButtonTitle:@"知道了"otherButtonTitles: nil]; [alert show]; //面这个方法可判断是否是因为用户拒绝访问地址所致,如果是中文环境返回 的是“用户拒绝访问” if (([myerror.localizedDescription rangeOfString:@"User denied access"].location!=NSNotFound )|| ([myerror.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound)) }]; }
  • 相关阅读:
    java调用存储过程(stored procedures)的HelloWorld例子
    java中JDBC当中请给出一个DataSource的HelloWorld例子
    java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子
    java当中JDBC当中Scrollable和Updatable ResultSet的用法和Helloworld例子
    java当中JDBC当中请给出一个sql server的helloworld例子
    java当中请给出一个oracle的helloworld例子
    java当中JDBC当中请给出一个sql server的stored procedure例子
    java当中JDBC当中请给出一个sql server的dataSource的helloworld例子
    java当中JDBC当中请给出一个SQLServer DataSource and SingleTon例子
    java当中JDBC当中请给出一个Oracle DataSource and SingleTon例子
  • 原文地址:https://www.cnblogs.com/allanliu/p/4184791.html
Copyright © 2020-2023  润新知