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);
});
});
}
Related
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);
}];
}
Usually when i want to read some image metadata in iOS i use imagePickerController to choose the image and Photos Framework and imagePickerController:didFinishPickingMediaWithInfo:
to get image info and extract the metadata like this
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImagePickerControllerSourceType pickerType = picker.sourceType;
if(pickerType == UIImagePickerControllerSourceTypePhotoLibrary)
{
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:#[url,] options:nil];
PHAsset *asset = fetchResult.firstObject;
[self metaDataFromPhotoLibrary:asset];
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
-(void)metaDataFromPhotoLibrary:(PHAsset*)asset{
// NSLog(#"Start metadata");
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
options.networkAccessAllowed = YES; //download asset metadata from iCloud if needed
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
NSDictionary *metadata = fullImage.properties;
NSMutableDictionary *imageMetadata = nil;
imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:metadata];
NSString *dateString = metadata [#"{TIFF}"] [#"DateTime"];
NSString *latitude = metadata [#"{GPS}"][#"Latitude"];
NSString *longitude = metadata [#"{GPS}"][#"Longitude"];
//etc etc etc
}
But can't do the same thing from an Action Extension
in the extension code i use a code like this to get the selected image
- (void)viewDidLoad {
[super viewDidLoad];
// Get the item[s] we're handling from the extension context.
// For example, look for an image and place it into an image view.
// Replace this with something appropriate for the type[s] your extension supports.
BOOL imageFound = NO;
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
// This is an image. We'll load it, then place it in our image view.
__weak UIImageView *immagine = self.immagine;
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
if(image) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[immagine setImage:image];
}];
}
}];
imageFound = YES;
break;
}
}
if (imageFound) {
// We only handle one image, so stop looking for more.
break;
}
}
}
Using UIImagePNGRepresentation or UIImageJPEGRepresentation i lost many metadata and i can read only a few data !
How can i get all image metadata from the image selected from action Extension ?
Thank you so mush
Note: i 've found some app in the AppsStore that read all metadata dictionary from extension , so there must be a solution for my problem !
Thank you again
Vanni
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;
}
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();
}];
}
}
I'm working on a simple puzzle game that use an image as background of the board game.
Everything works fine, but there are a little thing bothering me. When I take a photo from camera I can save it on my Photo Library but I don't know where it was saved, I mean, what is the exactly URL of my photo.
Here is the code where I save photos:
- (void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = imageToSave.size;
[scrollView setContentSize:bounds.size];
[scrollView setContentOffset:bounds.origin];
[imageView setFrame:bounds];
imageView.image = imageToSave;
if ([info objectForKey:#"UIImagePickerControllerReferenceURL"] == nil) {
// Save the new image (original or edited) to the Camera Roll
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
ALAssetOrientation orientation; //= [[[info objectForKey:#"UIImagePickerControllerMediaMetadata"] objectForKey:#"Orientation"] integerValue];
NSString *infoOrientation = [[info objectForKey:#"UIImagePickerControllerMediaMetadata"] objectForKey:#"Orientation"];
switch ([infoOrientation integerValue]) {
case 3:
orientation = ALAssetOrientationUp;
break;
case 6:
orientation = ALAssetOrientationRight;
break;
default:
orientation = ALAssetOrientationDown;
break;
}
[al writeImageToSavedPhotosAlbum:[imageToSave CGImage] orientation:orientation completionBlock:^(NSURL *assetURL, NSError *error) {
if (error == nil) {
NSLog(#"saved");
savedImageCam = assetURL;
} else {
NSLog(#"error");
}
}];
} else {
NSLog(#"URL from saved image: %#", savedImageCam);
NSLog(#"URL from photo image: %#", [info objectForKey:#"UIImagePickerControllerReferenceURL"]);
}
}
// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
The call method UIImageWriteToSavedPhotosAlbum doesn't return the URL, and the instance variable info just bring the URL if I call the Photo Library directly not through of camera.
Anybody knows how can I fix it?
Don't use UIImageWriteToSavedPhotosAlbum. Instead, use the ALAssetsLibrary and writeImageToSavedPhotosAlbum:orientation:completionBlock:. The completionBlock then gives you access to the asset URL that you can use to get the image again in the future.