保留照片到體系相冊
保留照片到體系相冊這個功效許多社交類的APP都有的,明天我們簡略講授一下,若何將圖片保留到體系相冊(Photo Album)。
1.創立UIImageView
創立UIImageView是為了將照片展現出來,我們是要把UIImage保留到體系相冊(Photo Album):
#define SCREEN [UIScreen mainScreen].bounds.size
self.image = [UIImage imageNamed:@"IOSDevTip"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN.width - 300) / 2, 70, 300, 150)];
imageView.image = self.image;
[self.view addSubview:imageView];
2.創立UIButton
創立UIButton並綁定actionClick:事宜:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake( 100, 300, SCREEN.width - 200, 40);
[button addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setTitle:@"SavePhoto" forState:UIControlStateNormal];
[self.view addSubview:button];
- (void)actionClick:(UIButton *)button
{
}
3.保留照片到體系相冊(Photo Album)
在actionClick:辦法裡挪用:
UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
這個時刻,我們想曉得保留能否勝利,所以須要制訂回調辦法
// 指定回調辦法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(!error){
NSLog(@"save success");
}else{
NSLog(@"save failed");
}
}
在這個辦法裡,我們就曉得照片能否保留勝利。
保留照片到本身創立的相簿
接上去,我們來具體講授一下關於體系相冊權限獲得、保留照片、創立本身的相簿等等功效。
1.創立本身的相簿
這也是一種比擬創立的作法,創立本身的相簿,然後把照片或許視頻保留到本身的相簿中。相干代碼以下:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//創立相簿勝利
} failureBlock:^(NSError *error) {
//掉敗
}];
2.保留照片
這個辦法也是將照片保留到體系相簿外面,不是保留到本身創立的相簿外面。代碼以下:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *asSetUrl,NSError *error){
if (error) {
//掉敗
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存儲勝利"
message:nil
delegate:nil
cancelButtonTitle:@"肯定"
otherButtonTitles:nil, nil];
[alert show];
}
}];
3.獲得權限
在保留照片之前,假如用戶封閉相冊權限,這個時刻是保留掉敗的。假如你不做任何處置,用戶是不會曉得本身保留掉敗了。所以,我們可以在保留照片之前,做出響應的提醒。若何獲得這個權限呢?普通有兩種辦法:
(1)創立相簿掉敗
(2)保留照片掉敗
在下面兩個辦法創立本身的相簿和保留照片的掉敗成果裡,我們可以彈出獲得照片權限掉敗的提醒。我們拿第一個創立相簿掉敗來舉例:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//創立相簿勝利
} failureBlock:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存儲掉敗"
message:@"請翻開 設置-隱私-照片 來停止設置"
delegate:nil
cancelButtonTitle:@"肯定"
otherButtonTitles:nil, nil];
[alert show];
}];
在保留照片掉敗的成果裡,我們也能夠彈出響應的提醒,讓用戶翻開運用法式的相冊權限。
4.保留照片到本身的相簿
上面這段代碼是在年夜谷歌外面找到的,親身測試過是可以用的,整頓以下:
#pragma mark - 創立相冊
- (void)createAlbum
{
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:@"IOSDevTip"])
{
haveHDRGroup = YES;
}
}
if (!haveHDRGroup)
{
//do add a group named "XXXX"
[assetsLibrary addAssetsGroupAlbumWithName:@"IOSDevTip"
resultBlock:^(ALAssetsGroup *group)
{
[groups addObject:group];
}
failureBlock:nil];
haveHDRGroup = YES;
}
}
};
//創立相簿
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];
[self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(self.image) customAlbumName:@"iOSDevTip" completionBlock:^
{
//這裡可以創立添加勝利的辦法
}
failureBlock:^(NSError *error)
{
//處置添加掉敗的辦法顯示alert讓它回到主線程履行,否則誰人框框逝世活不願彈出來
dispatch_async(dispatch_get_main_queue(), ^{
//添加掉敗普通是由用戶不許可運用拜訪相冊形成的,這邊可以掏出這類情形加以斷定一下
if([error.localizedDescription rangeOfString:@"User denied Access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用戶謝絕拜訪"].location!=NSNotFound){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil];
[alert show];
}
});
}];
}
- (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();
}
}
}];
}
【iOS保留App中的照片到體系相冊或自建相冊的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!