How to get UIImagePicker selected image name? - ios

Does anyone know how to get the selected image name (the one from photo gallery) after presenting an UIImagePickerController?

With the help of ALAsset you can able to achieve this.
Step 1: Get the Asset for the selected Image from the UIImagePicker
Step 2: Get the ALAssetRepresentation for that Asset.
Step 3: ALAssetRepresentation class has a property called fileName
- (NSString *)filename
Returns a string representing the filename of the representation on disk.
This Sample Code will Helps you...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
NSString *fileName = [representation filename];
NSLog(#"fileName : %#",fileName);
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
}
NOTE: It works only in iOS 5 and later.

Related

how to show UIImage file path on UILabel in objective c

I am new in iOS and I am facing problem regarding to show file path on UILabel. I am taking Image for gallery and I want to show Image name and its extension on UILabel.
My code is like this
Button Click...
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
imgPanCard.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
I need to show Image name and its extension on UILabel "No. File Chosen" How to do this.Thanks in Advance!
I just change code like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(#"[imageRep filename] : %#", [imageRep filename]);
lblPanCard.text=[imageRep filename];
[picker dismissViewControllerAnimated:NO completion:nil];
imgPanCard.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
};
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
}
Add following code in "didFinishPickingMediaWithInfo" method:
// get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(#"[imageRep filename] : %#", [imageRep filename]);
};
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

Get the image name from NSURL UIImagepicker Controller iOS 9

I have picked image from UIImagePickerController, after that i get the info dictionary of its delegate method. I want to get image name from NSURL Please suggest code for iOS 9, because ALAsset library code has been deprecated to get image name.
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
import asset library
Try This, You will get image name in cstmGetUploadImageName method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// selected image
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.img_profilepic.image = chosenImage;
// NSString *setimageName;
// get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(#"[imageRep filename] : %#", [imageRep filename]);
[self cstmGetUploadImageName:[imageRep filename]];
};
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
// upload image method
//[self cstmUploadImage:chosenImage withName:#"sdf"];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)cstmGetUploadImageName:(NSString *)filename{
NSString *tempFileName = filename;
NSLog(#"%#",tempFileName);
}

iOS: Does writeImageToSavedPhotosAlbum compress the origin image?

I'm using UImagePickerController to take a photo from the camera on iOS9. Then I found that using writeImageToSavedPhotosAlbum to save the image is different from saving the image using UIImageJPEGRepresentation at the file size. Code as follow:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)info {
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera ) {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error ) {
// NSLog(#"url: %#", assetURL.absoluteString);
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSString *path = [NSString stringWithFormat:#"%#%#", NSTemporaryDirectory(), #"/tmp.jpg"];
[data1 writeToFile:path atomically:YES];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
// CGImageRef iref = [rep fullResolutionImage];
NSLog(#"size1: %lu, size2: %lld", data1.length, rep.size);
} failureBlock:^(NSError *error) {
}];
}];
}
}
it prints:
2016-09-08 10:10:56.690658 TestPhotoes[6096:2345233] size1: 5549568, size2: 2288036
You see, the image file saved by UIImageJPEGRepresentation is way much bigger than by writeImageToSavedPhotosAlbum. I was wondering what the writeImageToSavedPhotosAlbum did? Does it compress the origin image? How can I save image using UIImageJPEGRepresentation to achieve the same effect, both image file size, and the image quality?

How to get the url of sending an image in IOS application

I am working with an IOS application in which when i selected an image from IPhone simulator i want to take the URL(url Path) for that image .when i am trying to do it with my below code i get an array of url and not getting the specific string value from that array .Please help me that in which way i am able to retrieve that url for the image only not the array which i am getting with me code.
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
NSString *fileName = [representation filename];
NSLog(#"fileName : %#",fileName);
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
NSLog(#"ALAssetsLibrary : %#",assetslibrary);
Please give me the solution that how can i retrieve the url for that image?
thanks in advance.

How to save path of image picked from UIImagePickerViewController to sqlite database and access it later ? ios

I need to save path of the image picked from server and later access the path to show that image on user click by database retrival.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *urlPath = [info objectForKey:#"UIImagePickerControllerReferenceURL"]absoluteString];
}
and saving it to database.
It saved as assets-library://asset/asset.JPG?id=E1136225-97DE-4BF4-A864-67E33D60737A&ext=JPG
then I want to import to Imageview
UIImageView *iv = [[UIImageView alloc]init];
iv.image = [UIimage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagepath]]];
But it is not working
Try this:
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref){
UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
[fileImage addObject:myImage];
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
//failed to get image.
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:[filePath objectAtIndex:0] resultBlock:resultblock failureBlock:failureblock];
Note: Make sure your [filePath objectAtIndex:0] will be a NSUrl object. Else convert it to NSUrl
Example:
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
NSURL myAssetUrl = [NSURL URLWithString:[filePath objectAtIndex:0]];
assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];
Use assetForURL:resultBlock:failureBlock: method of ALAssetsLibrary class to retrieve image by URL.
There is more info: http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009722
UPD:
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL: url resultBlock: ^(ALAsset *asset) {
ALAssetRepresentation *r = [asset defaultRepresentation];
self.imgView.image = [UIImage imageWithCGImage: r.fullResolutionImage];
}
failureBlock: nil];
Where is url is your cached URL

Resources