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 devise a method of ruin
// 指定回调方法- (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. Demo Address: Https://github.com/worldligang/iOSStrongDemo (or click "Read the original" There is a demo link address). The next article will tell you how to create your own albums and save your photos.
iOS Dev > Learning-Save photo to System album (Photo Album)