第一部分:保存到系統相冊php
IOS保存圖片功能只有一句話:UIImageWriteToSavedPhotosAlbum([erweima image], nil, nil,nil);ui
通常來講這就夠了,但有的時候須要根據保存反饋來進行下一步操做,能夠加以下方法:.net
/**blog
* save image to photo album completion method圖片
*get
* @param image saved imageit
* @param error saved error, if is not null that means save successio
* @param contextInfo I don't kown😄table
*/select
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error != NULL){
//失敗
}
else{
//成功
}
}
調用的時候這樣寫:UIImageWriteToSavedPhotosAlbum([erweima image], self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
我也是剛查的,參考博文:http://blog.cnrainbird.com/index.php/2012/07/11/uiimagewritetosavedphotosalbum_pan_duan_shi_fou_bao_cun_cheng_gong/
第二部分:建立自定義相冊,並保存圖片,代碼爲主,可直接賦值,可是有個bug,暫時沒找到怎麼處理,若是有會的大神請指教一下
/**
* 建立自定義相冊
Bug:詳情見此方法中 TODO
*/
-(void)createAFonderInAlbum {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
NSMutableArray *groups=[[NSMutableArray alloc]init];
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[groups addObject:group];
}
else
{
BOOL haveHDRGroup = NO;
for (ALAssetsGroup *gp in groups)
{
NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"你的相冊名"])
{
haveHDRGroup = YES;
}
}
if (!haveHDRGroup)
{
//do add a group named "XXXX"
[assetsLibrary addAssetsGroupAlbumWithName:@"你的相冊名"
resultBlock:^(ALAssetsGroup *group)
{
if (group == nil) {//TODO Bug,第一次建立OK,當我在相冊裏刪除剛建立的自定義相冊後,再次建立,此時 group == nil
return ;
}
[groups addObject:group];
}
failureBlock:nil];
haveHDRGroup = YES;
}
}
};
//建立相簿
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];
//保存圖片到剛建立的相冊中
[self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation([erweima image]) customAlbumName:@"你的相冊名" completionBlock:^{
//成功
} failureBlock:^(NSError *error) {
//失敗
}];
}
/**
* save image to custom album
*
* @param metadata I have not use it,just nil
* @param imageData image png data to save
* @param customAlbumName custom album name
* @param completionBlock finish block,success
* @param failureBlock finish block,fail
*/
- (void)saveToAlbumWithMetadata:(NSDictionary *)metadata
imageData:(NSData *)imageData
customAlbumName:(NSString *)customAlbumName
completionBlock:(void (^)(void))completionBlock
failureBlock:(void (^)(NSError *error))failureBlock
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
__weak ALAssetsLibrary *weakSelf = assetsLibrary;
void (^AddAsset)(ALAssetsLibrary *, NSURL *) = ^(ALAssetsLibrary *assetsLibrary, NSURL *assetURL) {
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
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) {
[weakSelf assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
if (completionBlock) {
completionBlock();
}
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
} else {
AddAsset(weakSelf, assetURL);
}
} failureBlock:^(NSError *error) {
AddAsset(weakSelf, assetURL);
}];
} else {
if (completionBlock) {
completionBlock();
}
}
}];
}
參考連接:http://www.jianshu.com/p/e6b5d55fd6ec