Key points for reading photo gallery and saving pictures or videos to photo gallery in IOS _ios

Source: Internet
Author: User
Tags file url

Read Photo Gallery Photolibrary
if we only need to read a picture or a video (or take a photo/video) Once in iOS, then we can do it with Uiimagepickercontroller. But most of the time we need to read multiple photos or videos from the photolibrary, we need to do a different thing, and Apple provides us with the appropriate interface.
Before we start coding we want to get to know a few classes:
Alassetslibrary: On behalf of the entire photolibrary, we can generate an instance object of it, which is equivalent to the handle of the photo gallery.
Alassetsgroup: A grouping of photo galleries, we can get all of the grouped handles by alassetslibrary instances.
Alasset: A Alasset instance represents an asset, which is a photo or video, and we can get the corresponding Subnail or original image from his instance, and so on.
Another thing to know is that blocks,apple in iOS 4.0 after the large number of this thing, there is more and more use of the meaning, but this thing really good. I'm not saying much about this stuff, I'm going to talk about my blog.
For the purposes of this article, we read the group and each asset is asynchronous, but we now use blocks we can do it in a function. So blocks is really convenient.
Let's look at the code directly below:

Copy Code code as follows:

Alassetslibrary *assetslibrary = [[Alassetslibrary alloc]init];//generate an instance of the entire photolibrary handle
Nsmutablearray *mediaarray = [[Nsmutablearray alloc]init];//Storage array for media
[Assetslibrary enumerategroupswithtypes:alassetsgroupall usingblock:^ (alassetsgroup *group, BOOL *stop) {//Get all Group
[Group enumerateassetsusingblock:^ (Alasset *result, Nsuinteger index, BOOL *stop) {//from group inside
nsstring* Assettype = [result Valueforproperty:alassetpropertytype];
if ([Assettype Isequaltostring:alassettypephoto]) {
NSLog (@ "Photo");
}else if ([Assettype Isequaltostring:alassettypevideo]) {
NSLog (@ "video");
}else if ([Assettype Isequaltostring:alassettypeunknown]) {
NSLog (@ "Unknow assettype");
}

Nsdictionary *asseturls = [result valueforproperty:alassetpropertyurls];
Nsuinteger assetcounter = 0;
For (NSString *asseturlkey in Asseturls) {
NSLog (@ "Asset URL%lu =%@", (unsigned long) assetcounter,[asseturls Objectforkey:asseturlkey]);
}

NSLog (@ "Representation Size =%lld", [[result Defaultrepresentation]size]);
}];
} failureblock:^ (Nserror *error) {
NSLog (@ "Enumerate the asset groups failed.");
}];

Save pictures or videos to Photolibrary
the file is then then transferred to the photo library via the path to the temporary file.
Let's take a look at the corresponding API directly:

Copy Code code as follows:

These methods can is used to add photos or videos to the saved photos.

With a uiimage, the APIs user can use-[uiimage Cgimage] to get a cgimageref, and cast-[uiimage imageorientation] to AL Assetorientation.
-(void) Writeimagetosavedphotosalbum: (cgimageref) imageref orientation: (alassetorientation) orientation Completionblock: (Alassetslibrarywriteimagecompletionblock) Completionblock;

The API user would have to specify the orientation key in the metadata dictionary to preserve the orientation of the IMA Ge
-(void) Writeimagetosavedphotosalbum: (cgimageref) Imageref metadata: (nsdictionary *) metadata completionblock: ( Alassetslibrarywriteimagecompletionblock) Completionblock __osx_available_starting (__MAC_NA,__IPHONE_4_1);

If There is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata ES would be overwritten
-(void) Writeimagedatatosavedphotosalbum: (NSData *) ImageData metadata: (nsdictionary *) metadata completionblock: ( Alassetslibrarywriteimagecompletionblock) Completionblock __osx_available_starting (__MAC_NA,__IPHONE_4_1);

-(void) Writevideoatpathtosavedphotosalbum: (Nsurl *) Videopathurl Completionblock: ( Alassetslibrarywritevideocompletionblock) Completionblock;

The first three are stored pictures, through the parameters we can find that the first use of the direction we passed in, the second can be passed through the image of the metadata to retain the image of the metadata, the first two are the picture into cgimageref and then save, The third is the incoming nsdata so you can completely retain the image of the information, but also metadata, if the image of the information with the metadata conflict that metadata will cover the picture itself with metadata.
The last one is the API that stores the video, and you can see that the parameter is a nsurl, as long as you're wearing a local temp file URL.
Store pictures According to your needs to choose the appropriate API, for example, we get a uiimage instance, then we use the first or second more convenient, if we read from the local temporary file image data then we directly use the third is more convenient.
Here's a simple piece of code:
Copy Code code as follows:

-(void) SaveImage: (uiimage*) image{
Alassetslibrary *assetslibrary = [[Alassetslibrary alloc]init];
[Assetslibrary writeimagetosavedphotosalbum:[image Cgimage] Orientation: (alassetorientation) Image.imageorientation completionblock:^ (Nsurl *asseturl, Nserror *error) {
if (Error) {
NSLog (@ "Save image fail:%@", error);
}else{
NSLog (@ "Save image succeed.");
}
}];
}

Saving video is a hassle, you need to write the video to a local file and then get to the path to the local temp file, and then call the fourth API above to write to the photo library.
About writing temporary files, I have written an article on the file read and write, you can go to see.
I am here to write a demo of the project resource library to photo library so that you can import the video into the simulator, which is convenient to test sometimes.
The main code is as follows:
Copy Code code as follows:

-(void) Save: (nsstring*) urlstring{
Alassetslibrary *library = [[Alassetslibrary alloc] init];
[Library Writevideoatpathtosavedphotosalbum:[nsurl Fileurlwithpath:urlstring]
completionblock:^ (Nsurl *asseturl, Nserror *error) {
if (Error) {
NSLog (@ "Save video fail:%@", error);
} else {
NSLog (@ "Save video succeed.");
}
}];
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.