Initialize UIImageView with a file name from the device library - ios

I have a picture named IMG_0094.JPG, taken by the device's camera.
I would like initialize my UIImageView with it but my code doesn't work.
Following code is in viewDidLoad.
currentImageName = [contact objectForKey:#"image"];
NSURL* aURL = [NSURL URLWithString:currentImageName];
NSLog(#"%#", aURL);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:aURL resultBlock:^(ALAsset *asset)
{
UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
UIImageView *photo = (UIImageView *)[self.view viewWithTag:TAG_PHOTO];
photo.image = copyOfOriginalImage;
photo.contentMode = UIViewContentModeScaleAspectFit;
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(#"failure---");
}];
My NSLOG return 2015-02-26 10:39:54.899 Fiche Contacts[1600:60b] IMG_0094.JPG

The answer, use the url which is generate when the picture is saved : http://www.to-string.com/2014/04/30/how-to-extract-string-reference-to-an-image-in-ios-and-then-load-the-image-later/

Related

iOS - display image from `ALAssetLibrary`

I have ViewController1 and ViewController2.
ViewController1 is UICollectionView and it use ASAssetLibrary to load thumbnail into UICollectionViewCell:
NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
ALAssetsLibrary *al = [TemplateEditBarController defaultAssetsLibrary];
[al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset) {
[collector addObject:asset];
}
}];
assetsarray = collector;//<--assetsarray is local.
}
failureBlock:^(NSError *error) { NSLog(#"fail!");}
];
When cell selected, I want to pass either NSString or NSURL to ViewController2 so that it will call and display the full resolution image. But when NSLog the url:
ALAsset* asset = backgroundsarray[row];
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSURL *url = [representation url];
newbg = [url absoluteString];//<-NSLog this.
I get:
assets-library://asset/asset.JPG?id=6E5438ED-9A8C-4ED0-9DEA-AB2D8F8A9360&ext=JPG
I tried change to [url path] I get:
asset.JPG
How can I get the actual photo url so that I can convert to NSString to pass to ViewController2?
NSURL* aURL = [NSURL URLWithString:imagePath];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:aURL resultBlock:^(ALAsset *asset)
{
UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
imageView.image=image;
}
failureBlock:^(NSError *error)
{
// error handling
NSLog(#"failure-----");
}];
My solution here:
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {
image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
}
}
Apparently there isn't any direct solution other than passing ALAsset. This is what I do:
In ViewController1, which is UICollectionViewController, I have this function:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(#"selected row: %ld",(long)indexPath.row);
long row = [indexPath row];
NSString* newbg = [[NSString alloc]init];
if ([bartype isEqualToString:#"photolibrary"]) {
ALAsset* asset = backgroundsarray[row];
[_delegate changeBackgroundWithAsset:asset];
}
Then I have a function in ViewController2 to process ALAsset:
-(void)changeBackgroundWithAsset:(ALAsset*)b{
ALAssetRepresentation *rep = [b defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *bimage;
bimage = [UIImage imageWithCGImage:iref];
UIImageView* bgview = [[UIImageView alloc]init];
bgview = curbgview;
bgview.image = bimage;
}

How to Display asset url In collection view ios

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result) {
[assets addObject:result];
ALAssetRepresentation *rep = [result defaultRepresentation];
photoname=[rep filename];
imageurl=[rep url];
NSLog(#"result %#",result);
NSLog(#"name %#",photoname);//Img0001.Jpg
NSLog(#"name %#",imageurl);//assets-library://asset/asset.JPG?id=3B36DBC8-A2F7-4088-ADEF-A9E2FF8FD927&ext=JPG
[print addObject:photoname];
//NsuserDefaults
// NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:print];
// [[NSUserDefaults standardUserDefaults] setObject:mutableArray forKey:#"test"];
retArray = [[NSMutableArray alloc] initWithArray:((NSMutableArray *) [[NSUserDefaults standardUserDefaults] objectForKey:#"test"])];
NSLog(#"res %#",retArray);
}
};
ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[self.assetsGroup setAssetsFilter:onlyPhotosFilter];
[self.assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];
}
You need to use the assets library to get the asset for the URL, and then get the image from the asset. Currently you are treating the asset URL as an asset rather than using the URL to get the asset:
[self.assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset * asset) {
UIImage *image = [UIImage imageWithCGImage:[asset thumbnail]];
// use the image for something
}
NSURL *assetURL = notequal[indexPath.row];
[self.assetslibrary assetForURL:assetURL resultBlock:^(ALAsset * asset) {
UIImage *image = [UIImageimageWithCGImage[asset thumbnail]];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:kImageViewTag];
imageView.image = thumbnail;
}];
return cell;

Get the Image (UIImage) using asset URL in iOS 7

I have some images in my Photo Album. I have already retrieved the asset urls of all of them.. I want to get a particular image using the asset url... below code gave me asset url..
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *URL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
Now using this url, how can I get the image(UIImage)?
Get Url by this,
NSURL *url= (NSURL*) [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
And get image from url like below.
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
#autoreleasepool {
CGImageRef iref = [rep fullScreenImage];
if (iref) {
UIImage *image = [UIImage imageWithCGImage:iref];
self.loadedImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
//UIMethod trigger...
});
iref = nil;
}
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Can't get image - %#",[myerror localizedDescription]);
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:yourUrl
resultBlock:resultblock
failureBlock:failureblock];
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]
Hope this helps

Get image name from UIImageWriteToSavedPhotosAlbum in ios [duplicate]

This question already has answers here:
Getting image name of iphone photo library
(2 answers)
Closed 9 years ago.
I've searched how to get the name of the saved image takes from the camera, but i didn't find something simple.
this is my code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
NSLog(#"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
{
NSLog(#"we have our ALAsset!");
NSLog(#"%#", assetURL);
}
failureBlock:^(NSError *error )
{
NSLog(#"Error loading asset");
}];
}];
}
}
I you have the answer, i will happy to test it .
Thanks in advance.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *resourceURL;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];
image =[info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSURL *imagePath = [info objectForKey:#"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSData *imageData;
NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:#"."].location+1 ];
if ([extensionOFImage isEqualToString:#"jpg"])
{
imageData = UIImagePNGRepresentation(image);
}
else
{
imageData = UIImageJPEGRepresentation(image, 1.0);
}
int imageSize=imageData.length/1024;
NSLog(#"imageSize--->%d", imageSize);
if (imageName!=nil) {
NSLog(#"imageName--->%#",imageName);
}
else
{
NSLog(#"no image name found");
}
}
If you are using ASSerts
-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
[self dismissViewControllerAnimated:YES completion:^{
if (assets.count < 1) return;
//self.pageControl.numberOfPages = assets.count;
int index = 0;
for (ALAsset *asset in assets) {
// NSString *imageName = [[asset defaultRepresentation] filename];
UIImage *image = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
// (#"%#", [[asset defaultRepresentation] filename]);
NSData *imageData = UIImagePNGRepresentation(image);
int imageSize=imageData.length/1024;
(either)
NSURL *imagePath = [asset objectForKey:#"UIImagePickerControllerReferenceURL"];
// NSURL *imagePath = [NSURL URLWithString:[asset ob]];
NSString *imageName = [imagePath lastPathComponent];
(or)
NSLog(#"%#",[[asset defaultRepresentation] filename]);
index++;
}
}];
}
Try following code. I have used this in one of my previous project and its working for me :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selectedImage = info[UIImagePickerControllerEditedImage];
self.photoView.image = _selectedImage;
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];
[picker dismissViewControllerAnimated:YES completion:NULL];
}

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