Multiple images not getting saved in Photo Library by using UIActivityViewController - ios

I need to save multiple images in the photo library, the user can multiple selects the images from the app gallery then can save them in iPhone Photo Gallery. I am showing the UIActivityViewController for the purpose.
Suppose a user selects 10 or more images and choose to save them into photo library then only 7-8 images are saved.
Is there any way by which i can save array of images in the photo library without any failure ?
Thanks
let images = Generic.fetchImagesFromMediaFiles(self.selectedMediaObj) // to fetch selected images
let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil);
if let popoverPresentationController = activityViewController.popoverPresentationController {
popoverPresentationController.sourceView = self.shareAllView
}

iOS system write photo save to album use single thread, one by one to do.
if you want to save more photos same time, it maybe loss some data.
-(void)saveBtn
{
[SSGOTools againRequestPhotoWithblock:^(BOOL isAgree) {
if (isAgree) {
self.listOfImages = [NSMutableArray new];
int photoNum ;
photoNum = (int)_photoArray.count;
if (_photoArray.count > 9) {
photoNum = 9;
}
for (int i = 0; i < photoNum; i++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_photoArray[i]]];
UIImage *myImage = [UIImage imageWithData:data];
//[self.listOfImages addObject:myImage];
[self loadImageFinished:myImage];
}
}
}];
}
- (void)loadImageFinished:(UIImage *)image
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//write photo save to album
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
NSLog(#"success = %d, error = %#", success, error);
if(success){
dispatch_async(dispatch_get_main_queue(), ^{
[SSGOTools showInfoPopHint:#"Success"];
});
}
}];
}

you will need to use the completion block here for ensuring all images are saved.. try this :
-(void)saveBtn{
[SSGOTools againRequestPhotoWithblock:^(BOOL isAgree) {
if (isAgree) {
self.listOfImages = [NSMutableArray new];
int photoNum ;
photoNum = (int)_photoArray.count;
if (_photoArray.count > 9) {
photoNum = 9;
}
for (int i = 0; i < photoNum; i++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_photoArray[i]]];
UIImage *myImage = [UIImage imageWithData:data];
[self.listOfImages addObject:myImage];
// [self loadImageFinished:myImage];
}
[self saveAllImages:self.listOfImages];
}
}];
}
-(void)saveAllImages:(NSMutableArray *)images {
UIImage *image = [images firstObject];
[images removeObject:image];
[self loadImageFinished:image :^(bool success) {
if (success){
if (images.count > 0){
[self saveAllImages:images];
}else{
// all images saved do whatever you want;
}
}else{
NSLog(#"failed saving image");
}
}];
}
- (void)loadImageFinished:(UIImage *)image :(void(^)(bool success))completion{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//write photo save to album
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
NSLog(#"success = %d, error = %#", success, error);
if(success){
dispatch_async(dispatch_get_main_queue(), ^{
[SSGOTools showInfoPopHint:#"Success"];
});
}
completion(success);
}];
}

Related

how to save an image to my own gallery [duplicate]

I'm creating an iOS 5 app. I want to save a photo to the device.
I want to save the photo to an album specific to my app, so I need to create the album, and then save photos to the album.
I know how to create the album:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:#"MY APP NAME" resultBlock:^(ALAssetsGroup *group) {
//How to get the album URL?
} failureBlock:^(NSError *error) {
//Handle the error
}];
I want add photos to the new album now, how do I do so? Sample code is greatly appreciated!
You may use the following code just change the name of album :
__weak ALAssetsLibrary *lib = self.library;
[self.library addAssetsGroupAlbumWithName:#"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
///checks if group previously created
if(group == nil){
//enumerate albums
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
//if the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"My Photo Album"]) {
//save image
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
//then get the image asseturl
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
//put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
}failureBlock:^(NSError *error){
}];
}else{
// save image directly to library
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error) {
}];
For anyone looking to do this as of iOS 9, things have gotten a bit more complicated since the ALAssetsLibrary is deprecated in favor of the new Photos library.
Here's some Swift code for adding UIImages to a specific album name (creating the album if it doesn't exist), you may need to do some refactoring/optimization for your needs:
func insertImage(image : UIImage, intoAlbumNamed albumName : String) {
//Fetch a collection in the photos library that has the title "albumNmame"
let collection = fetchAssetCollectionWithAlbumName(albumName)
if collection == nil {
//If we were unable to find a collection named "albumName" we'll create it before inserting the image
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
}, completionHandler: {(success : Bool, error : NSError?) in
if error != nil {
print("Error: " + error!.description)
}
if success {
//Fetch the newly created collection (which we *assume* exists here)
let newCollection = self.fetchAssetCollectionWithAlbumName(albumName)
self.insertImage(image, intoAssetCollection: newCollection!)
}
}
)
} else {
//If we found the existing AssetCollection with the title "albumName", insert into it
self.insertImage(image, intoAssetCollection: collection!)
}
}
func fetchAssetCollectionWithAlbumName(albumName : String) -> PHAssetCollection? {
//Provide the predicate to match the title of the album.
let fetchOption = PHFetchOptions()
fetchOption.predicate = NSPredicate(format: "title == '" + albumName + "'")
//Fetch the album using the fetch option
let fetchResult = PHAssetCollection.fetchAssetCollectionsWithType(
PHAssetCollectionType.Album,
subtype: PHAssetCollectionSubtype.AlbumRegular,
options: fetchOption)
//Assuming the album exists and no album shares it's name, it should be the only result fetched
let collection = fetchResult.firstObject as? PHAssetCollection
return collection
}
func insertImage(image : UIImage, intoAssetCollection collection : PHAssetCollection) {
//Changes for the Photos Library must be maded within the performChanges block
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
//This will request a PHAsset be created for the UIImage
let creationRequest = PHAssetCreationRequest.creationRequestForAssetFromImage(image)
//Create a change request to insert the new PHAsset in the collection
let request = PHAssetCollectionChangeRequest(forAssetCollection: collection)
//Add the PHAsset placeholder into the creation request.
//The placeholder is used because the actual PHAsset hasn't been created yet
if request != nil && creationRequest.placeholderForCreatedAsset != nil {
request!.addAssets([creationRequest.placeholderForCreatedAsset!])
}
},
completionHandler: { (success : Bool, error : NSError?) in
if error != nil {
print("Error: " + error!.description)
}
}
)
}
For those looking for Eddy's answer in Objective-C.
#import <Photos/Photos.h>
- (void)insertImage:(UIImage *)image intoAlbumNamed:(NSString *)albumName {
//Fetch a collection in the photos library that has the title "albumNmame"
PHAssetCollection *collection = [self fetchAssetCollectionWithAlbumName: albumName];
if (collection == nil) {
//If we were unable to find a collection named "albumName" we'll create it before inserting the image
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: albumName];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Error inserting image into album: %#", error.localizedDescription);
}
if (success) {
//Fetch the newly created collection (which we *assume* exists here)
PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName];
[self insertImage:image intoAssetCollection: newCollection];
}
}];
} else {
//If we found the existing AssetCollection with the title "albumName", insert into it
[self insertImage:image intoAssetCollection: collection];
}
}
- (PHAssetCollection *)fetchAssetCollectionWithAlbumName:(NSString *)albumName {
PHFetchOptions *fetchOptions = [PHFetchOptions new];
//Provide the predicate to match the title of the album.
fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"title == '%#'", albumName]];
//Fetch the album using the fetch option
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];
//Assuming the album exists and no album shares it's name, it should be the only result fetched
return fetchResult.firstObject;
}
- (void)insertImage:(UIImage *)image intoAssetCollection:(PHAssetCollection *)collection {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//This will request a PHAsset be created for the UIImage
PHAssetCreationRequest *creationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:image];
//Create a change request to insert the new PHAsset in the collection
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
//Add the PHAsset placeholder into the creation request.
//The placeholder is used because the actual PHAsset hasn't been created yet
if (request != nil && creationRequest.placeholderForCreatedAsset != nil) {
[request addAssets: #[creationRequest.placeholderForCreatedAsset]];
}
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Error inserting image into asset collection: %#", error.localizedDescription);
}
}];
}
Adaptation of Eddy's answer for Swift 4:
func saveImageToAlbum(_ image: UIImage, name: String) {
if let collection = fetchAssetCollection(name) {
self.saveImageToAssetCollection(image, collection: collection)
} else {
// Album does not exist, create it and attempt to save the image
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not create the album")
if let err = error {
NSLog("Error: \(err)")
}
return
}
if let newCollection = self.fetchAssetCollection(name) {
self.saveImageToAssetCollection(image, collection: newCollection)
}
})
}
}
func fetchAssetCollection(_ name: String) -> PHAssetCollection? {
let fetchOption = PHFetchOptions()
fetchOption.predicate = NSPredicate(format: "title == '" + name + "'")
let fetchResult = PHAssetCollection.fetchAssetCollections(
with: PHAssetCollectionType.album,
subtype: PHAssetCollectionSubtype.albumRegular,
options: fetchOption)
return fetchResult.firstObject
}
func saveImageToAssetCollection(_ image: UIImage, collection: PHAssetCollection) {
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.creationRequestForAsset(from: image)
if let request = PHAssetCollectionChangeRequest(for: collection),
let placeHolder = creationRequest.placeholderForCreatedAsset {
request.addAssets([placeHolder] as NSFastEnumeration)
}
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not save the image")
if let err = error {
NSLog("Error: " + err.localizedDescription)
}
return
}
})
}
Improved version on Objective C, using blocks. It creates an album, if it doesn't exist, then saves three types of media items - photos, gifs and videos:
// Types of media, that can be saved to an album
typedef NS_ENUM(NSUInteger, AlbumMediaType) {
AlbumMediaTypePhoto,
AlbumMediaTypeGIF,
AlbumMediaTypeVideo
};
/**
Creates album if it doesn't exist and returns it in a block
*/
- (void)createCollectionOnComplete:(void (^ _Nonnull)(PHAssetCollection * _Nonnull collection))onComplete
{
NSString *albumTitle = #"YOUR_ALBUM_TITLE";
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
// Searching for an existing album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:#"title = %#", albumTitle];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:fetchOptions].firstObject;
// If album is not found, we create it
if (!collection)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumTitle];
placeholder = [createAlbum placeholderForCreatedAssetCollection];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:#[placeholder.localIdentifier]
options:nil];
collection = collectionFetchResult.firstObject;
// After creating album, we return it
onComplete(collection);
}
}];
} else {
// If album already exists, we instantly return it
onComplete(collection);
}
}
/**
Saves an item of a given mediatype, that is located in mediaURL
*/
- (void)saveToAlbumMediaItemFromURL:(NSURL *)mediaURL mediaType:(AlbumMediaType)mediaType
{
NSData *mediaData = [NSData dataWithContentsOfURL:mediaURL];
if (!mediaData) {
OWSFail(#"%# Could not load data: %#", self.logTag, [self.attachmentStream mediaURL]);
return;
}
[self createCollectionOnComplete:^(PHAssetCollection * _Nonnull collection) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// We create a PHAsset using creationRequest
PHAssetCreationRequest *assetRequest;
switch (mediaType) {
case AlbumMediaTypePhoto: {
assetRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:[UIImage imageWithData:mediaData]];
break;
}
case AlbumMediaTypeGIF: {
assetRequest = [PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
[assetRequest addResourceWithType:PHAssetResourceTypePhoto data:mediaData options:options];
break;
}
case AlbumMediaTypeVideo: {
if ( !UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(mediaURL.path) ) {
OWSFail(#"%# Could not save incompatible video data.", self.logTag);
break;
}
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"file.mov"];
[mediaData writeToFile:videoPath atomically:YES];
assetRequest = [PHAssetCreationRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:videoPath]];
break;
}
default:
break;
}
// Creating a request to change an album
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
// PHAsset is not created yet, so we use a placeholder
PHObjectPlaceholder *placeholder = [assetRequest placeholderForCreatedAsset];
// We add a placeholder of a created item to the request of changing album
if (albumChangeRequest != nil && placeholder != nil) {
[albumChangeRequest addAssets: #[placeholder]];
}
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"Media item saved!");
} else {
NSLog(#"Error saving media item - %#", error ? error.localizedDescription : #"");
}
}];
}];
}
We can use these methods to save media items this way:
[self saveToAlbumMediaItemFromURL:[self.attachmentStream mediaURL] mediaType:AlbumMediaTypeGIF];

Always save screenshot in same custom name app album in "Photos"

I am building an app with button that make screen shot. Now I want to save all screen shots made in this app, in same custom name app album. I already know how to create album just the first time that app opens with code bellow.
I am creating my album with Photos framework like:
-(void)createAppAlbum
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:#"App Album Name"];
albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
}
completionHandler:^(BOOL success, NSError *error) {
if (success) {
fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:#[albumPlaceholder.localIdentifier] options:nil];
assetCollection = fetchResult.firstObject;
}
else {
NSLog(#"Error creating album: %#", error);}
}];
}
So my app creates album named "App Album Name".
I am able to take and store my screenshot in my new album with button like:
-(IBAction)screenshot:(id)sender
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[assetCollectionChangeRequest addAssets:#[[assetChangeRequest placeholderForCreatedAsset]]];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(#"Error creating asset: %#", error);
}
}];
}
In case I leave this viewController, and get back in later, I want to find album that I already created, and save another screen shot in same album, because I don't want to create new one every time I enter this view controller.
So my question is, how would I get album I created first time by Name and then save new screen shots in.
Since this question is specifically targeting, iOS 8, this code will work just fine:
- (void)saveImage:(UIImage *)image {
if (!self.library) {
self.library = [[ALAssetsLibrary alloc] init];
}
__weak ALAssetsLibrary *lib = self.library;
[self.library addAssetsGroupAlbumWithName:#"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
///checks if group previously created
if(group == nil){
//enumerate albums
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
//if the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"My Photo Album"]) {
//save image
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
//then get the image asseturl
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
//put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
}failureBlock:^(NSError *error){
}];
}else{
// save image directly to library
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error) {
}];
}
To test that this works, I created a single screen application, added a button to the view, and added a method that takes a screenshot and then passes the image to the saveImage: function. Here's the code that I added to it to make it work:
#import <AssetsLibrary/AssetsLibrary.h>
#interface ViewController ()
#property (nonatomic, strong) UIButton *screenshotButton;
#property (nonatomic, strong) ALAssetsLibrary *library;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.screenshotButton];
}
- (UIButton *)screenshotButton {
if (!_screenshotButton) {
_screenshotButton = [[UIButton alloc] initWithFrame:CGRectInset(self.view.bounds, 100.0f, 120.0f)];
_screenshotButton.layer.borderColor = [UIColor blueColor].CGColor;
_screenshotButton.layer.borderWidth = 2.0f;
_screenshotButton.layer.cornerRadius = 5.0f;
[_screenshotButton setTitle:#"Take Screenshot" forState:UIControlStateNormal];
[_screenshotButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_screenshotButton addTarget:self action:#selector(takeScreenshot) forControlEvents:UIControlEventTouchUpInside];
}
return _screenshotButton;
}
- (void)takeScreenshot {
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imgData = UIImagePNGRepresentation(image);
if(imgData)
[imgData writeToFile:#"screenshot.png" atomically:YES];
else
NSLog(#"error while taking screenshot");
UIColor *randomColor = [UIColor colorWithHue:((CGFloat)(arc4random()%255)/255.0f)
saturation:((CGFloat)(arc4random()%255)/200.0f)
brightness:((CGFloat)(arc4random()%100)/255.0f) + 0.5f
alpha:1.0f];
self.screenshotButton.layer.borderColor = randomColor.CGColor;
[self.screenshotButton setTitleColor:randomColor forState:UIControlStateNormal];
[self saveImage:image];
}
A few things to note:
AssetsLibrary is deprecated in iOS 9, so it is not recommended to
be used after iOS 8, but will still work.
This can be implemented and
used with any UIImage passed to saveImage:, so it can work even
if you're using another library.
Make sure to include #import <AssetsLibrary/AssetsLibrary.h>.
Change "My Photo Album" to the correct album name, and possibly set that statically

use PhotoEditing Extension to make effects on images

I want to set cropped image in PhotoEditing Extension
I provided that facility for cropping and rotate facility now I want to make that effect on Photos of simulator
// Adjustment data
PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self.strSelectedFilterName];
PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:#"com.test.PhotoEditingExtensionDemo"
formatVersion:#"1.0"
data:archivedData];
contentEditingOutput.adjustmentData = adjustmentData;
switch (self.input.mediaType)
{
case PHAssetMediaTypeImage:
{
// Get full size image
NSURL *url = self.input.fullSizeImageURL;
int orientation = self.input.fullSizeImageOrientation;
// Generate rendered JPEG data
UIImage *image = [UIImage imageWithContentsOfFile:url.path];
image = [self transformedImage:image withOrientation:orientation usingFilter:self.ciFilter];
NSData *renderedJPEGData = UIImageJPEGRepresentation(image, 0.9f);
// Save JPEG data
NSError *error = nil;
BOOL success = [renderedJPEGData writeToURL:contentEditingOutput.renderedContentURL options:NSDataWritingAtomic error:&error];
if (success) {
completionHandler(contentEditingOutput);
} else {
NSLog(#"An error occured: %#", error);
completionHandler(nil);
}
break;
}
case PHAssetMediaTypeVideo: {
// Get AV asset
AAPLAVReaderWriter *avReaderWriter = [[AAPLAVReaderWriter alloc] initWithAsset:self.input.avAsset];
avReaderWriter.delegate = self;
// Save filtered video
[avReaderWriter writeToURL:contentEditingOutput.renderedContentURL
progress:^(float progress) {
}
completion:^(NSError *error) {
if (!error) {
completionHandler(contentEditingOutput);
} else {
NSLog(#"An error occured: %#", error);
completionHandler(nil);
}
}];
break;
}
default:
break;
}

UIImagePickerControllerReferenceURL always returns nill

I am trying to get the name of the image which I have just captured from camera with following code. But [info objectForKey:#"UIImagePickerControllerReferenceURL"] always returning nil. How can I get the URL?
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.myinfo = info;
NSLog(#"Dismissing camera ui...");
[self.cameraUI dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Getting media url...");
NSString *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"Media url = %#", mediaURL);
NSLog(#"Getting media type...");
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSLog(#"Selected mediaType: %#", mediaType);
if(mediaURL) {
NSLog(#"This is a video = %#", mediaURL);
if (![mediaType isEqualToString:(NSString*)kUTTypeVideo]) {
UISaveVideoAtPathToSavedPhotosAlbum(mediaURL, self, #selector(video:didFinishSavingWithError:contextInfo:), NULL);
}
} else {
NSLog(#"This is a photo...");
self.originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (self.source == UIImagePickerControllerSourceTypeCamera && [mediaType isEqualToString:(NSString*)kUTTypeImage]) {
// Image captured from camera
NSLog(#"Saving new image...");
if (self.source != UIImagePickerControllerSourceTypePhotoLibrary) {
UIImageWriteToSavedPhotosAlbum(self.originalImage, self,
#selector(image:didFinishSavingWithError:usingContextInfo:), nil);
}
}
// Image selected from previous images.
else {
NSLog(#"Getting reference url...");
self.referenceURL = [info objectForKey:#"UIImagePickerControllerReferenceURL"];
NSLog(#"Reference url = %#", [self.referenceURL absoluteString]);
[self saveAssetData:self.originalImage :info];
}
}
}
- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
usingContextInfo:(void*)ctxInfo {
if (error) {
NSLog(#"Resim kaydedilemedi: %#", [error localizedDescription]);
NSString *title = #"Resim kaydedilemedi!";
NSString* message = #"Resim kaydedilirken hata oluştu!";
[self alertStatus:message:title];
} else {
NSLog(#"Save asset data...");
[self saveAssetData:image :self.myinfo];
}
}
- (void)saveAssetData:(UIImage*)originalImage :(NSDictionary*)info {
self.assetLibrary = [[ALAssetsLibrary alloc] init];
NSURL *url = [info objectForKey:#"UIImagePickerControllerReferenceURL"];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
{
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
NSString *filename = [assetRep filename];
NSLog(#"File name = %#", filename);
if(self.selectedMediaNames == nil)
self.selectedMediaNames = [[NSMutableArray alloc] init];
[self.selectedMediaNames addObject:filename];
[self.tableView reloadData];
[self.activitIndicator stopAnimating];
[self.activitIndicator setHidden:true];
HMXSharedDataManager *sharedDataManager =
[HMXSharedDataManager sharedManager];
[sharedDataManager.uploaMedias addObject:originalImage];
[sharedDataManager.uploaMediaNames addObject:filename];
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error)
{
NSLog(#"%#", error);
};
[self.assetLibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}
UPDATE:
It is a little bit late but here how I get the name of the image or video:
Check UIImagePickerControllerMediaURL, if it is null the media is an image if not it is a video
If the image or the video is just taken or recorded save it to photos album
Use ALAssetsLibrary to query file name.
Here is the code for saving and getting media:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
#try {
[self.cameraUI dismissViewControllerAnimated:YES completion:nil];
mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
// If mediaURL is not null this should be a video
if(mediaURL) {
// This video is new just recorded with camera
if (self.source == UIImagePickerControllerSourceTypeCamera) {
// First save the video to photos album
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
DDLogDebug(#"Failed to save the photo to photos album...");
} else {
// Get the name of the video
[self getMediaName:nil url:assetURL];
}
}];
} else { // This is a video that recorded before
// Get the name of the video
[self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
}
}
// This is an image
else {
self.originalImage = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];
// This image is new just taken with camera
if (self.source == UIImagePickerControllerSourceTypeCamera) {
// First save the image to photos album
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library writeImageToSavedPhotosAlbum:[self.originalImage CGImage]
orientation:(ALAssetOrientation)[self.originalImage imageOrientation]
completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
DDLogDebug(#"Failed to save the vide to photos album...");
} else {
// Get the name of the image
[self getMediaName:self.originalImage url:assetURL];
}
}];
} else { // This is an image that taken before
// Get the name of the image
[self getMediaName:self.originalImage
url:[info objectForKey:#"UIImagePickerControllerReferenceURL"]];
}
}
}
#catch (NSException *exception) {
DDLogError(#"%#", [exception description]);
}
}
Actual method that gets the media name:
- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
#try {
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
if (asset == nil) return;
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
NSString *fileName = [assetRep filename];
// Do what you need with the file name here
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) {
DDLogError(#"Failed to get image or video name : %#", error);
};
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}
#catch (NSException *exception) {
DDLogError(#"%#", [exception description]);
}
}
The image that you capture with the camera from within the application has no name. It is always nil. You have to programmatically save that image in the photo gallery and you can save with any name you want.
Put the following code in didFinishPickingMediaWithInfo:
NSURL *mediaUrl;
NSString *imageURLString;
self.selectImage = [info valueForKey:UIImagePickerControllerEditedImage];
if (mediaUrl == nil) {
if (self.selectImage == nil) {
self.selectImage = [info valueForKey:UIImagePickerControllerOriginalImage];
DebugLog(#"Original image picked.");
}else {
DebugLog(#"Edited image picked.");
}
}
mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
imageURLString=[mediaUrl absoluteString];
DebugLog(#"Hi Image URL STRING : - %#",imageURLString);
if ([StringUtils string:imageURLString contains:#"PNG"] || [StringUtils string:imageURLString contains:#"png"]) {
self.isJPG = NO;
self.profileImageName = #"profileImageName.png";
} else if ([StringUtils string:imageURLString contains:#"JPG"] || [StringUtils string:imageURLString contains:#"jpg"]) {
self.isJPG = YES;
self.profileImageName = #"profileImageName.jpg";
}
When you set camera for kUTTypeMovie , then only you will get referenceurl and mediaurl. It will return null for kUTTypeImage.
For Xamarin.iOS developers: store image capture from camera and get its data using ALAssetsLibrary
var originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
var meta = e.Info[UIImagePickerController.MediaMetadata] as NSDictionary;
//Get image bytes
if (originalImage != null)
{
using (NSData imageData = originalImage.AsPNG())
{
myByteArray = new Byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
}
//This bit of code saves image to the Photo Album with metadata
ALAssetsLibrary library = new ALAssetsLibrary();
library.WriteImageToSavedPhotosAlbum(originalImage.CGImage, meta, (assetUrl, error) =>
{
library.AssetForUrl(assetUrl, delegate (ALAsset asset)
{
ALAssetRepresentation representation = asset.DefaultRepresentation;
if (representation != null)
{
string fileName = representation.Filename;
var filePath = assetUrl.ToString();
var extension = filePath.Split('.')[1].ToLower();
var mimeData = string.Format("image/{0}", extension);
var mimeType = mimeData.Split('?')[0].ToLower();
var documentName = assetUrl.Path.ToString().Split('/')[1];
}
}, delegate (NSError err) {
Console.WriteLine("User denied access to photo Library... {0}", err);
});
});
}

Go from image url rep from asset library to base64

I am ultimately trying to convert an array of photos that are in a dictionary in url rep form to base64 to be sent over json.
Here is the dictionary code and log of it:
NSDictionary *dict = [self.form dictionaryWithValuesForKeys:keys];
NSLog(#"dict::%#",dict);
NSLog:
dict::{
boardLodgingFurnished = "<null>";
caption = "<null>";
cars = "";
photos = (
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=CE8A426B-3B59-4172-8761-CC477F3BB3EE&ext=JPG";
},
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=F4B68A42-1CA0-4880-9FB5-177CB091A28C&ext=JPG";
}
);
yearsAtLocation = "";
yearsInTheBusiness = "";
}
So for each photo in the dictionary I would like to take the urlRep and convert that to a base64 string and replace the urlRep with it in the dictionary.
What I have right now..not sure if I am going in the right direction:
for (id imageURL in [dict objectForKey:#"photos"])
{
ALAssetsLibrary *library = [ALAssetsLibrary new];
ALAsset *ourAsset = [self assetForURL:imageURL withLibrary:library];
/* Check out ALAssets */
NSLog(#"%#", ourAsset);
ALAssetRepresentation *representation = [ourAsset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];
//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSLog(#"imagedata??%#", [imageData base64EncodedString]);
//need to know how to add this back to dict
}
the below method is called from above but crashes on the while loop with
-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090
2014-01-03 10:57:27.361 Inspection App[2728:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090'
method
- (ALAsset *)assetForURL:(NSURL *)url withLibrary:(ALAssetsLibrary *)assetsLibrary {
__block ALAsset *result = nil;
__block NSError *assetError = nil;
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
result = asset;
dispatch_semaphore_signal(sema);
} failureBlock:^(NSError *error) {
assetError = error;
dispatch_semaphore_signal(sema);
}];
if ([NSThread isMainThread]) {
while (!result && !assetError) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
else {
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
return result;
}
edit:
if (photoUrls.count) {
for (id photos in photoUrls){
NSString *urlString = photos;
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {
NSLog(#"imagedata??%#", base64);
}];
}
}
else {
NSLog(#"where are my urls?");
}
NSMutableDictionary *jsonWithPhotos = [dict mutableCopy];
[jsonWithPhotos setObject:convertedImages forKey:#"photo64"];
NSLog(#"jjson photos::%#", jsonWithPhotos);
updated method
- (void)base64ImageAtUrlString:(NSString *)urlString result:(void (^)(NSString *))completion {
NSURL *url = [NSURL URLWithString:urlString];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:^(ALAsset *asset) {
// borrowing your code, here... didn't check it....
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];
//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSString *base64 = [imageData base64EncodedString];
completion(base64);
[convertedImages addObject:base64];
// NSLog(#"converted::%#",convertedImages);
} failureBlock:^(NSError *error) {
NSLog(#"that didn't work %#", error);
}];
}
When i log jsonWithPhotos the object photo64 is just a blank array
The crash is due to a false assumption in the code about the dictionary. Given the posted description of the dictionary parsed as json, you'd need to get the urls like this:
// collect the photo urls in an array
NSMutableArray *photoUrls = [NSMutableArray array];
// photos is an array of dictionaries in the dictionary
NSArray *photos = dict[#"photos"];
for (NSDictionary *photo in photos) {
// photo is a dictionary containing a "caption" and a "urlRep"
[photoUrls addObject:photo[#"urlRep"]];
}
Now you can proceed with a method whose job is just the conversion. Your question might contain a lot more questions about how to do this. I'd recommend starting simple. See if you can do one conversion. Test it by writing the reverse, from base64 back to an image.
Edit 0: Without deeply checking it, I'd restructure your encoding attempt to look like this:
- (void)base64ImageAtUrlString:(NSString *)urlString result:(void (^)(NSString *))completion {
NSURL *url = [NSURL URLWithString:urlString];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:^(ALAsset *asset) {
// borrowing your code, here... didn't check it....
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];
//TODO: Deal with JPG or PNG
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
NSString *base64 = [imageData base64EncodedString];
completion(base64);
} failureBlock:^(NSError *error) {
NSLog(#"that didn't work %#", error);
}];
}
Call it like this:
if (photoUrls.count) {
NSString *urlString = photoUrls[0];
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {
NSLog(#"imagedata??%#", base64);
}];
} else {
NSLog(#"where are my urls?");
}
Once it's working, see if you can reverse it, making an image out of the base64 data. Finally, once all that's working, you can deal with potential memory issues. My advice there is to consider encoding one at a time, post one at a time to the server and release everything in between.
Edit 1 - Per followup question, if you want to replace all of the urls in the url array with base64 encodings, it might go something like this (remember that this might use a lot of memory):
- (void)base64ImagesAtUrls:(NSMutableArray *)urls result:(void (^)(void))completion {
__block NSInteger completed = 0; // this is how we'll know that we're done
// this approach doesn't depend on the asset library retrievals completing
// sequentially, even though they probably will
for (int i=0; i<urls.count; i++) {
NSString *urlString = urls[i];
[self base64ImageAtUrlString:urlString result:^(NSString *base64) {
[urls replaceObjectAtIndex:i withObject:base64];
if (++completed == urls.count) completion();
}];
}
}

Resources