Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

Tuesday, 27 December 2016

CAMediaPicker

CAMediaPicker works like a UIImagePickercontroller. Its allow to select multiple images at a time from photo albums and photo libraries using "Photos Framework". It is a simple library and easy to integrate.

Features of CAMediaPicker
CAMediaPicker has a 4 options to select/add media 
1. MediaPickerSourcePhotoTypeAlbum: Allow to select multiple images at a time form photo albums and libraries.
2. MediaPickerSourceTypeImage:  Click live photo.
3. MediaPickerSourceVideoTypeAlbum: Allow to select a video from video libraries.
4. MediaPickerSourceTypeVideo: Record a live video.

Integration of CAMediaPicker:
1. First add all classes of CAMediaPicker to project.
2. Import "CAMediaManager.h" class to controller.
#import "CAMediaManager.h"
3. Create an instance of CAMediaManager.
CAMediaManager *mediaManager = [CAMediaManager sharedMediaManagerWithRootController:self];
4. Set delegate for CAMediaManage.
mediaManager.mediaPickerDelegate = self;
5. Present CAMediaPicker with setting MediaPickerSourceType
[mediaManager presentMediaPickerControllerWithSource:source];
6. Define delegate methods.

CAMediaPicker return selected media to MediaPickerDelegate.
MediaPickerDelegate contain 3 simple methods.
1.-(void) mediaPicker:(CAMediaManager *)mediaPicker didFailWithAuthorizationStatus:(PHAuthorizationStatus)status;
Above delegate method give current authentication that app can use application is authorized or not  to access photo data.

2. -(void) mediaPicker:(CAMediaManager *)mediaPicker didSelectedAssets:(NSArray *)assetList
Above delegate method give all selected media. It will give multiple images in case of selecting images from photo album, in the rest of the case it will give only one object.

3.-(void) didCancelMediaPicker:(CAMediaManager *)mediaPicker
This delegate method call, if user cancel selection of media.

 Sample code for display photo libraries and photo album(for selecting images)
CAMediaManager *mediaManager = [CAMediaManager sharedMediaManagerWithRootController:self];
    mediaManager.mediaPickerDelegate = self;
    [mediaManager presentMediaPickerControllerWithSource:MediaPickerSourcePhotoTypeAlbum]; 


Sample code for click a photo
CAMediaManager *mediaManager = [CAMediaManager sharedMediaManagerWithRootController:self];
    mediaManager.mediaPickerDelegate = self;
    [mediaManager presentMediaPickerControllerWithSource:MediaPickerSourceTypeImage]; 


Sample code for display list of all videos
 CAMediaManager *mediaManager = [CAMediaManager sharedMediaManagerWithRootController:self];
    mediaManager.mediaPickerDelegate = self;
    [mediaManager presentMediaPickerControllerWithSource:MediaPickerSourceVideoTypeAlbum]; 


Sample code for record a video
CAMediaManager *mediaManager = [CAMediaManager sharedMediaManagerWithRootController:self];
    mediaManager.mediaPickerDelegate = self;
    [mediaManager presentMediaPickerControllerWithSource:MediaPickerSourceTypeVideo];


Delegate methos
-(void) mediaPicker:(CAMediaManager *)mediaPicker didFailWithAuthorizationStatus:(PHAuthorizationStatus)status{
    if (status == PHAuthorizationStatusRestricted) {
        NSLog(@"Application not having access to use photo album and camera");
    }
   
}


-(void) mediaPicker:(CAMediaManager *)mediaPicker didSelectedAssets:(NSArray *)assetList{
     NSLog(@"Selected assets count = %zd", assetList.count);
}

-(void) didCancelMediaPicker:(CAMediaManager *)mediaPicker{
    NSLog(@"Cancel media selection");
}


Click here for download Sample project

Monday, 19 December 2016

Get All Album and Assets

ALAssets library has deprecated in iOS9, and Apple introduce new framework  'Photos Framework' for retrieve assets for display and playback, edit image or video content, or work with collections of assets such as album, moments, and iCloud Shared Album.

Features:
1. Fetching objects and requesting changes
    Class's instance of Photos framework model retrieve assets(images, videos,  and Live Photos), collections of assets(albums or moments), and list of collections(such as album folder or moment clusters). There objects are read only, immutable and contain metadata only.
2. Change observing.
3. Support for Photos app features.
4. Assets and thumbnail loading and caching.
5. Assets content editing.

For details about Photos framework Click here

In this part of blog we will fetch all video assets from device(Photos app).
PHAssets:
All images, videos and live photos in Photos library represented by an instance of PHAssets.
 Instances of PHAsset are immutable.

PHFetchResult:
An ordered list of assets or collections returned from Photos fetch methods.
Photos provide all assets in the in a fetch result.

PHImageManager:
PHImageManager provide methos for retrieving and generating preview thumbnails and full-size image or video data associated with assets instance.

PHAssetCollection:
PHAssetCollection is collection of Photos and Videos assets , such as a moment, user-created album, smart album.

Get All Collection List:

1. Get list of smart albums
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in smartAlbum){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


2. Get list of User created albums
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in userAlbums){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


Above code return all available list of collection of assets.

Now we can retrieve all assets from collection list.
here code for fetching all assets from collection list

 //initialize a mutable array for contain all assets
self.allImageAssetArray = [[NSMutableArray alloc] init];
 //Initialize an object of image manager
PHImageManager *manager = [PHImageManager defaultManager];
//fetch all image assets from collection list
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollectionIdentifier options:nil];
    NSLog(@"Total Count = %zd", assetsFetchResult.count);
    for (PHAsset *asset in assetsFetchResult){

    //Get image from Asset
        [manager requestImageForAsset:asset targetSize:thumbnailSize contentMode:PHImageContentModeAspectFit options:PHImageRequestOptionsResizeModeNone resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//add image to array
            [self.allImageAssetArray addObject:result];
        }];
    }
//end for loop

Friday, 4 March 2016

UIImagePickerController in Swift

UIImagePickerController  class manages customizable, system-supplied user interface for taking pictures and movies, and for choosing saved images and movies for use in the app. A UIImagePickerController manages user interactions and delivers the results of those interactions to a delegate object.
For more details Click here.

To use an image picker controller with in the app, perform these steps:

1) Verify that the device is capable of picking content from the desired source.
2) Check which media types are available for the source type using.
3) Adjust image picker controller's UI according to media type
4) Present user interface.


Code:

// MARK: - Open Camera
    func openCamera(source:UIImagePickerControllerSourceType){


//Check source type available or not
        if(UIImagePickerController.isSourceTypeAvailable(source) == false){
            return;
        }

//Create new object of UIImagePickerContrller
        let imagepickerController = UIImagePickerController()
       imagepickerController.sourceType = source
        imagepickerController.delegate = self

//Present UIImagePickerContrller user interface
        self.presentViewController(imagepickerController, animated: true, completion: nil)
    }


//MARK: - Image Picker Delegate
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        nameTitlelabel.hidden = false
        //dismiss image picker controller view
        picker.dismissViewControllerAnimated(true, completion: nil)
    }


    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

//Get selected image
        let seletctedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        patientImageView.image = seletctedImage

//dismiss image picker controller view
        picker.dismissViewControllerAnimated(true, completion: nil)
    }