iOS development Save photos to System album (Photo Album)
Save photos to System albums This feature is available in many social apps, so let's talk about how to save images to the system album (Photo Album) today.
Create Uiimageview
Created to UIImageView
show the photo, we are going to save it UIImage
to the system album (Photo Album):
#define SCREEN [UIScreen mainScreen].bounds.sizeself.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];
Create UIButton
To create UIButton
and bind an actionClick:
event:
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{}
Save photo to System album (Photo Album)
Called in the actionClick:
method:
UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
This time, we want to know if the save is successful, so we need to make a callback method
// 指定回调方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if(!error){ NSLog(@"save success"); }else{ NSLog(@"save failed"); }}
In this method, we know whether the photo was saved successfully. Then, refresh the UI thread as needed. The first time users use the app, when they click SavePhoto
the button, the system will ask permission:
Demo Address: Iosstrongdemo Next article will tell you how to create your own albums and save photos.
The source of this article was just online: http://www.superqq.com/blog/2015/08/03/ioskai-fa-zhi-bao-cun-zhao-pian-dao-xi-tong-xiang-ce-( Photo-album)/
iOS development Save photos to System album (Photo Album)