Get UID URL for ALAsset with ALAssetPropertyAssetURL - ios

I get asset URL by calling [asset valueForProperty:ALAssetPropertyAssetURL] sometimes it returns nil or may be asset is nil (I've seen it in the crash logs).
I've read in the documentation:
ALAssetPropertyAssetURL
The key to retrieve a URL identifier for the asset.
The corresponding value is an NSURL object.
This URL is used by the library-change notifications to identify assets and asset groups. Only the ALAssetRepresentation and ALAssetsGroup classes support this property.
I think it is mean, that I can't get ALAssetPropertyAssetURL property from ALAsset, but it works any time when I run it.
Questions:
How I can get Asset url to use it in [PHAsset fetchAssetsWithALAssetURLs: ....]?
UPD
I don't understand this: "How to get URL if only the ALAssetRepresentation and ALAssetsGroup classes support this property?"

We could use something like this with ALAsset object
[[asset defaultRepresentation] url]

Related

what is avurlasset work and simplyfie this mehtod?

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:assetURL options:#{AVURLAssetPreferPreciseDurationAndTimingKey:#YES}];
// audio asset url create object and specifies its method....
this audio asset can get value in only second?
I want to know how above code works and role of AVURLAsset
AVURLAsset is a subclass of AVAsset. AVAsset is used to get information about the asset such as metadata (like track titles, author, composer, and so on). The "asset" is usually a sound or video file.
In the example you have given, you are creating an AVAsset from a URL, and you are specifying that you want precise duration and timing. Because of that option, when you ask for the duration, there may be a significant amount of work required for the AVAsset method to compute a precise time. This is clearly stated in the Apple documentation. The documentation also suggests that you usually do not need the precise duration and timing.
The (no-)difference between AVAsset and AVURLAsset as of 2022-12-08:
https://developer.apple.com/documentation/avfoundation/avurlasset
This class is a concrete subclass of AVAsset. When you create an asset as shown below, the system creates and returns an instance of AVURLAsset.
let url: URL = // A local or remote asset URL.
let asset = AVAsset(url: url)

PhotoKit iOS8 - Retrieve image using the "PHImageFileURLKey"

Is there anyway I can use the Path returned from the "PHImageFileURLKey" to go into the photo library and retrieve the image?
The path returned is in this format:
"file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG"
My plan is to store this path in the database and use it to fetch the image when I need to get it back.
Any help is appreciated. Thank you!
I think your solution of retrieving Photo Kit asset from the URL is wrong.
Here is what I would do (supposing you have access to PHAsset):
Store the localIdentifier:
PHAsset *asset = /*Your asset */
NSString *localIdentifier = asset.localIdentifier;
//Now save this local identifier, or an array of them
When it is time to retrieve them you simply do:
PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:savedLocalIdentifiers options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//this gets called for every asset from its localIdentifier you saved
}];
If you only have access to “PHImageFileURLKey” then disregard this answer.
This isn't documented, so I'd strongly advise against using that URL for anything more than a prototype app. That said, this does appear to work:
dispatch_queue_t queue = dispatch_queue_create("photoLoadQueue", 0);
dispatch_async(queue, ^{
NSURL *privateUrl = [NSURL URLWithString:#"file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
NSData *imageData = [NSData dataWithContentsOfURL:privateUrl];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithData:imageData];
});
});
Naturally you'll need to replace the string used to initiate the url with one which is valid for your phone.
There are probably a load of issues with doing this - it's just not how the framework is meant to be used. Here are some off the top of my head:
When running in the simulator, the root path changes regularly between launches of the app, so if you store absoluteUrls like this your database will quickly become full of dead URLs. This will be inconvenient to say the least.
Worse, the URLs for the images may change on a real device - you don't have control over it, and once they change it's your app's fault for making the user reselect them or whatever.
You're not going to ever find out about changes to the PHAsset which the photo came from.
This may be circumventing user permission for photo access - what happens if your app's permission to access photos is revoked? This is probably an issue with lots of approaches to storing photos for later use, however.
You don't control the file - what if the user deletes it?
If I were you, I would retrieve the image properly from the photos framework, using PHImageManager requestImageForAsset: targetSize: contentMode: options: resultHandler:, and store it in a file within your app's directory, at a sensible resolution for whatever you're doing with it. This still doesn't give you asset changes, but is a pretty good solution.
If you want to store the assets themselves and only request the images when you actually need them, it might be worth investigating transient asset collections, though I've not used them so that might not work for what you need.

get video name AlAssetLibrary

I want to get video names that i am playing from gallery in MPMoviePlayer, I have searched a lot about getting videos name that are played from gallery, but i always get only path, can i get name of video?what should i do? any idea? i saw examples for video path only like this
ALAsset - Type:Video, URLs:{
"com.apple.quicktime-movie" = "assets-library://asset/asset.MOV?id=336068EA-C1B1-481C-82DA-F2419561A91A&ext=MOV";
In this path what will be the video name?
Try this
ALAssetRepresentation *assetRep = [anAssetItem defaultRepresentation];
NSString *fileName = [assetRep filename];
Returns a string representing the filename of the representation on disk.
Note:
For representations synced from iTunes, this will be the filename of the representation on the host.

Check if NSURL is an Asset

I have a NSURL class member wich can point to a local file, to a URL or to an Asset. I've found out that I have to use the Asset Library to load an Asset instead of:
[NSData dataWithContentsOfURL:nsurl];
So my Question is: How can I check if my NSURL object points to an Asset?
I've tried the search, but I've only how to check if it's a local file :(
PS: My App has to be iOS4 compatible
Assuming the URL is well-formed, it is an Assets Library URL iff [[url scheme] isEqualToString:#"assets-library"].

how to get the physical path to the ALAsset object

Is there a way to turn the asset url returned by the following call to the physical path on the device?
NSURL *assetUrl = [_selectedAsset defaultRepresentation] url];
There is no way. (And even if you had the path to file, your app could not read the file because of sandboxing.)
The only way to get the raw data of an asset is getBytes:fromOffset:length:error: in ALAssetRepresentation.

Resources