Save Photos to System albums
save photos to the System Photo album this feature is available in many social networking apps, and today we will briefly explain how to save the picture to the System album (Photo Album).
1. Create Uiimageview
To create the Uiimageview is to show the photos, we are going to save the UIImage to the System album (Photo Album):
Copy Code code as follows:
#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. Create UIButton
Create UIButton and bind Actionclick: events:
Copy Code code as follows:
UIButton *button = [[UIButton alloc] init];
Button.frame = CGRectMake (+, 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. Save photos to System albums (Photo Album)
Called in the Actionclick: method:
Copy Code code as follows:
Uiimagewritetosavedphotosalbum (Self.image, Self, @selector (image:didFinishSavingWithError:contextInfo:), NULL);
This time, we want to know if the save is successful, so we need to develop a callback method
Copy Code code as follows:
Specifying callback methods
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo
{
if (!error) {
NSLog (@ "save Success");
}else{
NSLog (@ "save Failed");
}
}
In this way, we know whether the photo was saved successfully.
Save photos to the albums you created
Next, we will explain in detail about the system album permissions to get, save photos, create their own albums and so on.
1. Create your own albums
This is also a way to create your own albums, and then save photos or videos to your albums. The relevant code is as follows:
Copy Code code as follows:
Alassetslibrary *library = [[Alassetslibrary alloc] init];
[Library addassetsgroupalbumwithname:@ "Test" resultblock:^ (Alassetsgroup *group) {
Create album success
} failureblock:^ (Nserror *error) {
Failed
}];
2. Save Photos
This method is also to save the photo to the system album, not to Save to the album you created. The code is as follows:
Copy Code code as follows:
Alassetslibrary *library = [[Alassetslibrary alloc]init];
[Library Writeimagetosavedphotosalbum:image. Cgimage Orientation: (alassetorientation) image.imageorientation completionblock:^ (NSURL *asSetUrl,NSError *error) {
if (Error) {
Failed
}else{
Uialertview *alert = [[Uialertview alloc]initwithtitle:@ ' storage success '
Message:nil
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil, nil];
[Alert show];
}
}];
3. Get permission
If the user closes the album permission before saving the photo, the save fails. If you do not do any processing, the user will not know that they have failed to save. So, we can make a corresponding prompt before we save the photo. How do I get this permission? There are generally two ways:
(1) failed to create album
(2) Failed to save photo
In the above two ways to create your own albums and save photos of the failed results, we can pop up to get photos permission failed to prompt. Let's take the first one to create a photo album failure to illustrate:
Copy Code code as follows:
Alassetslibrary *library = [[Alassetslibrary alloc] init];
[Library addassetsgroupalbumwithname:@ "Test" resultblock:^ (Alassetsgroup *group) {
Create album success
} failureblock:^ (Nserror *error) {
Uialertview *alert = [[Uialertview alloc]initwithtitle:@ ' store failed '
message:@ "Please open settings-privacy-photos to set"
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil, nil];
[Alert show];
}];
In the failure to save the photos, we can also pop-up the corresponding prompts to allow users to open the application album permissions.
4. Save photos to your albums
The following code is found in the big Google, personally tested is available, sorted as follows:
Copy Code code as follows:
#pragma mark-Create albums
-(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;
}
}
};
Create a photo album
[Assetslibrary enumerategroupswithtypes:alassetsgroupalbum Usingblock:listgroupblock FailureBlock:nil];
[Self Savetoalbumwithmetadata:nil imagedata:uiimagepngrepresentation (self.image) customalbumname:@ "IOSDevTip" completionblock:^
{
Here you can create a method to add success
}
failureblock:^ (Nserror *error)
{
Handling add failed methods show alert Let it go back to the main thread, or the box won't bounce.
Dispatch_async (Dispatch_get_main_queue (), ^{
Add failure is usually caused by the user does not allow the application access to the album, this can be taken out of this situation to judge
if ([Error.localizeddescription rangeofstring:@ "User denied access"].location!= Nsnotfound | | [error.localizeddescription rangeofstring:@ user denied access to '].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 ();
}
}
}];
}