Failed to load image from resource url
I have a class that stores information about assets on a phone (images, videos).
My class has ResourceURLString
, defined as such
@property NSURL *ResourceURL;
I am setting a property while looping through assets
on the phone as such
Item.ResourceURLString = [[asset valueForProperty:ALAssetPropertyURLs] objectForKey:[[asset valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]];
When the user clicks on the image, I want to load the image.
The code I have is this
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[CurrentItem.ResourceURL absoluteString]]];
Img = [UIImage imageWithData:imageUrl];
But the image is always null I have verified that the ResourceURL property contains the url of the assets: library://asset/asset.JPG?id=82690321-91C1-4650-8348-F3FD93D14613&ext=JPG
source to share
You cannot upload images this way.
You need to use the class for this ALAssetsLibrary
.
Add the assetslibrary infrastructure to your project and add the header files.
Use the below code to load the image:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
yourImageView.image = largeImage;
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};
NSURL *asseturl = [NSURL URLWithString:yourURL];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
source to share
Deprecated in iOS 9.0 ALAssetsLibrary
. Since iOS 8.0, this works with PHPhotoLibrary. This is a small extension to UIImage, Swift 2X. This uses a fixed image size.
import Photos extension UIImageView { func imageFromAssetURL(assetURL: NSURL) { let asset = PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil) guard let result = asset.firstObject where result is PHAsset else { return } let imageManager = PHImageManager.defaultManager() imageManager.requestImageForAsset(result as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: PHImageContentMode.AspectFill, options: nil) { (image, dict) -> Void in if let image = image { self.image = image } } } }
Getting ImageReferenceURL from UIImagePickerController delegate:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL
}
Image adjustment
let imageView = UIImageView()
imageView.imageFromAssetURL(imageURL)
There may be effects that I haven't encountered yet, the classic would be the UITableViewCell or thread issue. I will keep this update and appreciate your feedback.
source to share
Since iOS 8 you can use Photos Framework , here's how to do it in Swift 3
import Photos // use the Photos Framework
// declare your asset url
let assetUrl = URL(string: "assets-library://asset/asset.JPG?id=9F983DBA-EC35-42B8-8773-B597CF782EDD&ext=JPG")!
// retrieve the list of matching results for your asset url
let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil)
if let photo = fetchResult.firstObject {
// retrieve the image for the first result
PHImageManager.default().requestImage(for: photo, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: nil) {
image, info in
let myImage = image //here is the image
}
}
Use PHImageManagerMaximumSize if you want to get the original image size. But if you want to get smaller or specific size, you can replace PHImageManagerMaximumSize withCGSize(width:150, height:150)
source to share