How to Get Url for a Phasset

Getting the URL Path of an Image from device in iOS Swift

Use asset.requestContentEditingInput insted of asset.requestImagebelow to retrive image URL from PHAsset:

func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]){

let count = assets.count
for asset : PHAsset in assets{
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
if let input = eidtingInput, let photoUrl = input.fullSizeImageURL {
self.selectedPhotos.append(photoUrl)
}
}
}
}

Getting url for PHAsset

You should use PHImageManager.

Use the class's sharedInstance and call this method with your PHAsset, options, and a completion block handler:

- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler

The handler will give you an AVAsset you should use for your AVPlayerItem, as opposed to a URL.

Example:

[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
// Use the AVAsset avAsset
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
}];

Beware its likely asynchronous which will interfere with your application flow further.

How to get image url from PHAsset for iOS 13

asset.requestContentEditingInput(with: nil, completionHandler: { (input, info) in
if let input = input {
print(input.fullSizeImageURL) // file:///xxx
}
})

this is how i was able to get full imageURL.

How To get Image URL From PHAsset? Is it possible To save Image using PHAsset URL to document Directory?

asset = Here you have to pass your PHAsset .

PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
[[PHImageManager defaultManager]
requestImageDataForAsset:asset
options:imageRequestOptions
resultHandler:^(NSData *imageData, NSString *dataUTI,
UIImageOrientation orientation,
NSDictionary *info)
{
NSLog(@"info = %@", info);
if ([info objectForKey:@"PHImageFileURLKey"]) {

NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
// if you want to save image in document see this.
[self saveimageindocument:imageData withimagename:[NSString stringWithFormat:@"DEMO"]];
}
}];

-(void) saveimageindocument:(NSData*) imageData withimagename:(NSString*)imagename{

NSString *writePath = [NSString stringWithFormat:@"%@/%@.png",[Utility getDocumentDirectory],imagename];

if (![imageData writeToFile:writePath atomically:YES]) {
// failure
NSLog(@"image save failed to path %@", writePath);

} else {
// success.
NSLog(@"image save Successfully to path %@", writePath);
}

}
+ (NSString*)getDocumentDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}

Check image that landscape mode or portrait mode

if (chooseimage.size.height >= chooseimage.size.width)
{
Islandscape = NO;
}else{
UIImage* landscapeImage = [UIImage imageWithCGImage:chooseimage.CGImage
scale:chooseimage.scale
orientation:UIImageOrientationLeft];
self.imgPreviewView.image = landscapeImage;
self.imgPreviewView.contentMode = UIViewContentModeScaleAspectFill;
Islandscape = YES;
}

Add this permission into info.plist file

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your photo library to let you select a picture.</string>

Get an edited photo's URL from PHAsset

Try changing your return to "false"

If your block returns true, Photos provides the original asset data
for editing. Your app uses the adjustment data to alter, add to, or
reapply previous edits. (For example, an adjustment data may describe
filters applied to a photo. Your app reapplies those filters and
allows the user to change filter parameters, add new filters, or
remove filters.)

If your block returns false, Photos provides the most recent asset
data—the rendered output of all previous edits—for editing.

https://developer.apple.com/documentation/photos/phcontenteditinginputrequestoptions/1624055-canhandleadjustmentdata

 let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return false
}

asset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
guard let url = contentEditingInput?.fullSizeImageURL else {
observer.onError(PHAssetError.imageRequestFailed)
return
}
/// Using this `url`
})

How to get an ALAsset URL from a PHAsset?

Create the assetURL by leveraging the localidentifier of the PHAsset.
Example:
PHAsset.localidentifier returns 91B1C271-C617-49CE-A074-E391BA7F843F/L0/001

Now take the 32 first characters to build the assetURL, like:

assets-library://asset/asset.JPG?id=91B1C271-C617-49CE-A074-E391BA7F843F&ext=JPG

You might change the extension JPG depending on the UTI of the asset (requestImageDataForAsset returns the UTI), but in my testing the extensions of the assetURL seems to be ignored anyhow.



Related Topics



Leave a reply



Submit