How to Call pictures in an album in IOS app and get a recent picture _ios

Source: Internet
Author: User
Tags time interval

Uiimagepickercontroller from photos, galleries, photo albums to get pictures

There are three ways to get a picture of IOS:

1. Direct Call camera photo

2. Choose from the album

3. From the gallery to select

Uiimagepickercontroller is a system-supplied interface for capturing pictures and videos;

Use the Uiimagepickercontroller class to get picture video, roughly divided into the following steps:

1. Initialization of the Uiimagepickercontroller class;

2. Set the data source type of the Uiimagepickercontroller instance (explained below);

3. Set up proxy;

4. If you need to do picture modification, set allowsediting =yes.

There are three types of data sources:

Copy Code code as follows:

enum {
Uiimagepickercontrollersourcetypephotolibrary,//From image gallery
Uiimagepickercontrollersourcetypecamera,//From camera
Uiimagepickercontrollersourcetypesavedphotosalbum//From album
};

When using these sources it is best to check if the following devices are supported;
Copy Code code as follows:

if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera])
{
NSLog (@ "Support camera");
}
if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary])
{
NSLog (@ "Support gallery");
}
if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypesavedphotosalbum])
{
NSLog (@ "Support photo gallery");
}

Call the camera to get the resources
Copy Code code as follows:

-(void) Viewdidload {
[Super Viewdidload];
Picker = [[Uiimagepickercontroller alloc]init];
Picker.view.backgroundColor = [Uicolor Orangecolor];
Uiimagepickercontrollersourcetype sourchetype = Uiimagepickercontrollersourcetypecamera;
Picker.sourcetype = Sourchetype;
Picker.delegate = self;
picker.allowsediting = YES;
}

It's just an example. Uiimagepickercontroller and its attributes require pop-up calls when they need to get a picture
Copy Code code as follows:

[Self Presentviewcontroller:picker animated:yes completion:nil];

We also need agents to get the pictures we selected

Uiimagepickercontrollerdelegate
A total of three methods in the agent one of the 3.0 has been scrapped, and there's only two we need.

Copy Code code as follows:

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary
*) info;

Called when the user chooses to complete;
Copy Code code as follows:

-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker;

Called when the user cancels the selection;
Copy Code code as follows:

-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker
Didfinishpickingmediawithinfo: (nsdictionary *) info;

The selected information is in info, and info is a dictionary.

Keys in the dictionary:

    • NSString *const uiimagepickercontrollermediatype; Specifies the type of media selected by the user (last extension of the article)
    • NSString *const uiimagepickercontrolleroriginalimage; original picture
    • NSString *const uiimagepickercontrollereditedimage; modified picture
    • NSString *const uiimagepickercontrollercroprect; cutting dimensions
    • NSString *const uiimagepickercontrollermediaurl; URL of the media
    • NSString *const uiimagepickercontrollerreferenceurl; The URL of the original
    • NSString *const uiimagepickercontrollermediametadata; This value works when the data source is a camera.


get the most recent picture

Recent needs require me to imitate this feature of the latest picture of micro-letter chat tips.
Let's talk about ideas first.
The idea is very simple, click the "+" number to get a list of albums, get the latest saved a picture. According to the picture save time, the current timestamp is calculated and the interval time is obtained. So as to determine whether it is the time interval of demand. (Time interval customization)
Calculation formula: Current time-picture save time <= time interval
Then start writing a classification based on this idea.
It is about the acquisition of Alassetslibrary, then write a classification according to him.
But after the iOS9 the library is obsolete, but it is still a package, and then create a new tool class to fit on the line.

Copy Code code as follows:

Alassetslibrary+wj.h
#import <AssetsLibrary/AssetsLibrary.h>
@interface Alassetslibrary (WJ)
/**
* Get the latest photo
*
* @param block Callback
*/
-(void) Latestasset: (Void (^_nullable) (Alasset * _nullable asset,nserror error)) block;
@end
Alassetslibrary+wj.m
#import "Alassetslibrary+wj.h"
@implementation Alassetslibrary (WJ)
-(void) Latestasset: (void (^) (Alasset * _nullable, Nserror *_nullable)) block {
[Self Enumerategroupswithtypes:alassetsgroupsavedphotos usingblock:^ (alassetsgroup *group, BOOL *stop) {
if (group) {
[group Setassetsfilter:[alassetsfilter Allphotos]];
[Group enumerateassetswithoptions:nsenumerationreverse/* Traversal Mode */usingblock:^ (alasset *result, NSUInteger index, BOOL * Stop) {
if (result) {
if (block) {
Block (Result,nil);
}
*stop = YES;
}
}];
*stop = YES;
}
} failureblock:^ (Nserror *error) {
if (Error) {
if (block) {
Block (Nil,error);
}
}
}];
}
@end

In IOS8 Photokit already existed. So later write iOS8 the above application can be used directly.
Photokit simple and practical encapsulation. For detailed performance, see the link provided by the reference.
Copy Code code as follows:

Phasset+wj.h
#import <Photos/Photos.h>
@interface Phasset (WJ)
/**
* Get the latest photo
*/
+ (Phasset *) Latestasset;
@end
Phasset+wj.m
#import "Phasset+wj.h"
@implementation Phasset (WJ)
+ (Phasset *) Latestasset {
Gets a collection of all resources, sorted by the time the resource was created
Phfetchoptions *options = [[Phfetchoptions alloc] init];
Options.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@ "CreationDate" Ascending:NO]];
Phfetchresult *assetsfetchresults = [Phasset fetchassetswithoptions:options];
return [Assetsfetchresults Firstobject];
}
@end

According to the requirements of this article does not provide other functional writing (PS: The main I have just used >.<).
The image save time can be obtained in the corresponding asset (Phasset,alasset).

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.