I am working on PHAsset and NSURL on Swift.
I have an image url, for example file:///var/mobile/Media/DCIM/100APPLE/IMG_0043.JPG.
How can i make a PHAsset from this NSURL in Swift?
A PHAsset object represents an image or video file that appears in the Photos app, including iCloud Photos content. To display or edit assets, use the PHAsset class to fetch asset objects for the photos or videos you want to work with. An asset object is immutable and contains only the metadata for the photo or video it represents.
Thus you can not make PHAsset object.
Here is only one constructor for making object.
let asset = PHAsset()
There is no constructor for NSURL :-
This is what we frequently use:-
let imgData = NSData(contentsOfURL: (NSURL(string: "file:///var/mobile/Media/DCIM/100APPLE/IMG_0043.JPG"))!)
let image = UIImage(data: imgData!)
Related
I am getting the user selected image in PHAsset format. and I want to get the image path.
My goal is to upload the image to Firebase and based their docs, I need to have the image path.
I found from researching that I need to get the metadata of the image first and store it in local file and then I can retrieve the URL. Is it correct?
My question here is (If the above correct), how can I get the metadata of PHAsset image format?
If you want to retrive the image file Path from your PHAsset address then use this function:
[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];
asset=PHAsset *asset
I used UIImageWriteToSavedPhotosAlbum to save image to Camera Roll, but I want to get a reference to get back to the same image. Is it possible? I have a link (albeit in Japanese) that says it's possible to get the file path, but as of what I understand this is during selection (using imagePicker) and not before/after taking a picture. What I want is to get the reference of the image, save it in CoreData, and the use it later to fetch the image.
Edit: It seems as if there is an answer already but in the comments it says that ALAssetsLibrary is already deprecated and we should use PHPhotoLibrary instead.
You Can Get All the Photos, Videos and Other Data With Using PHAsset Library. Here is Code to get All Videos in Array .
//For Video
allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
For Image you Can use this
//For Images
allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
and You can Display it Using PHAsset in UiTableview or Any Other Control.
Here is the URL of Demo ,
AVPlayer Demo (Fetch Video With PHAsset Library)
just pass the file name and extension to fetch the image
+(UIImage *)loadImage:(NSString *)fileName ofType:(NSString *)extension {
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#.%#", documentsDirectoryPath, fileName, extension]];
return result; }
I'm building an app and I need to get the PHAsset of an image from the NSItemProvider.
The NSURL I'm getting is for example:
file:///Users/user/Library/Developer/CoreSimulator/Devices/84BDFE8E-127E-4677-83B8-208227CCBC16/data/Media/PhotoData/OutgoingTemp/A4D845E7-17E3-4C35-8993-027092D3604A.JPG
And I need to be able to get a PHAsset from that URL so I can use PHPhotoLibrary to work with the image.
I tried:
let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
Where imageURL is an NSURL with the previous posted URL but it always returns an empty array.
I am a newbie in iOS.I want to show a video thumbnail in image view such that by clicking a Button it should open a gallery and by selecting a video it should display the video thumbnail...... please help me...
There are videos and images that stored on photo library, firstly you want to get all videos from the library, prior to iOS 8, this post shows you how to fetch pictures using AssetsLibrary library. You can filter videos out when doing it, then you get an ALAsset, which can represent a image or video, suppose when you select a video, you get a ALAsset named asset, using the below function will get you a thumbnail image for that video.
If you use ALAsset.
UIImage *thumbnail = [UIImage imageWithCGImage:[asset thumbnail]];
For iOS 8, apple introduces a new Photos library,you can fetch all videos from the library as above, a video or images is represented by PHAsset, suppose you have a PHAsset ivar named asset, you can use the following method to get a UIImage from this asset. Take a look at requestImageForAsset:targetSize:contentMode:options:resultHandler:, you can set the target size to the size of the thumb image you want,in the resultHandler,you get the UIImage.
I am using below code to get thumbnail of video which is downloaded in document directory,
- (UIImage *)thumbnailFromVideoAtURL:(NSURL *)url
{
AVURLAsset *aImgAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *aImgGenerator = [[AVAssetImageGenerator alloc] aImgAsset];
aImgGenerator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef aImgRef = [aImgGenerator copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *aImgThumb = [[UIImage alloc] initWithCGImage:aImgRef];
return aImgThumb;
}
I am using ALAsset framework for selecting multiple images from the image library. I want to store the selected images into the database. These stored images are then to be used to show the slideshow of the images. My problem i am storing the image URL into the database, URL is in format - assets-library://asset/asset.JPG?id=1000000007&ext=JPG. Now for displaying the images i need UIImage object, so how do i create a UIImage object from this kind of URL. I have tried -
for(NSDictionary *dict in info)
{
NSString* path = [[NSString alloc] initWithString:[dict objectForKey:#"UIImagePickerControllerReferenceURL"]];
UIImage* ui;
ui = [[UIImage alloc] initWithContentsOfFile:path];
}
This did not worked. Please some one tell me how do i do it..