save image in camera roll and get asset url - iphone

for an app I'm developing, I use UIImagePickerController to shoot a picture and store it in camera roll:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
//... some stuff ...
UIImageWriteToSavedPhotosAlbum([info objectForKey:#"UIImagePickerControllerOriginalImage"], nil, nil, nil);
}
the image is saved, now I need to get its reference url so I try to enumerate camera roll and get the last image, but I always get the image before the one I just shot.
Anybody has an idea how to get the reference of the just saved picture?
Thanks,
Max

This solution should fix the problem:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:((UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]).CGImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"assetURL %#", assetURL);
}];
et voila:
assetURL assets-library://asset/asset.JPG?id=1611E84C-24E2-4177-B49A-1C57B4A9C665&ext=JPG

Related

How to save video from HTTP to photo library in ios?

I want to download some video from HTTP into user's ios device. How could we use AVAssetsLibrary to do that? I tried but got the following error:
Video /wikipedia/commons/4/4d/F1_vid2.theora.ogv cannot be saved to the saved photos album: Error Domain=NSOSStatusErrorDomain Code=2 "This movie could not be played." UserInfo=xxxxxxxx {NSLocalizedDescription=This movie could not be played.}
Thanks a bunch!
My code is:
ALAssetsLibrary *lib = [[[ALAssetsLibrary alloc] init] autorelease];
if ([lib videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) {
[lib writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error) {
if (!error) {
result = KIFTestStepResultSuccess;
NSLog(#"yuanzi: Completed writing videos.");
}
saved = YES;
}];
}

Save UIImage to Photo Album with writeImageToSavedPhotosAlbum

I'm trying to save a UIImage to Photo Album. I've tried severl methods the last one is:
-(IBAction)captureLocalImage:(id)sender{
[photoCaptureButton setEnabled:NO];
// Save to assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum: imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
{
// report_memory(#"After writing to library");
if (error2) {
NSLog(#"ERROR: the image failed to be written");
}
else {
NSLog(#"PHOTO SAVED - assetURL: %#", assetURL);
}
runOnMainQueueWithoutDeadlocking(^{
// report_memory(#"Operation completed");
[photoCaptureButton setEnabled:YES];
});
}];
}
imageView is a UIImageView which contain the image I want to save.
On log I got "PHOTO SAVED - assetURL: (null)" and the photo doesn't save to library.
What am I doing wrong?
just use this bellow line for save the image in your photo library
UIImageWriteToSavedPhotosAlbum(imageView.image,nil,nil,nil);
:)
For iOS 11+ needs Privacy - Photo Library Additions Usage Description in info.plist
Add NSPhotoLibraryAddUsageDescription in info.plist
Code:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Reference
Check out this sample.. which explains how to save photos using assets library..
you can even use..
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
cehck out his link How to save picture to iPhone photo library?
I'm using GPUImage, Apparently there is some kind of problem with the library itself.
The code above is absolutely working. If you want to read more about this issue:
GPUImagePicture with imageFromCurrentlyProcessedOutput doesn't get UIImage
Since iOS 8.1 UIKit framework has the UIImageWriteToSavedPhotosAlbum() function as vishy said.
I use it in this way:
UIImageWriteToSavedPhotosAlbum(myImage, self, "image:didFinishSavingWithError:contextInfo:", nil)
and then
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
// check error, report image has been saved, ...
}
When you take a photo from camera, then you can save image in picker delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
[library writeImageToSavedPhotosAlbum:image.CGImage metadata:[info objectForKey:UIImagePickerControllerMediaMetadata] completionBlock:^(NSURL *assetURL, NSError *error) {
...
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}

using UISaveVideoAtPathToSavedPhotosAlbum

On simulator UISaveVideoAtPathToSavedPhotosAlbum cannot save movie into photo album unless photo album is open. Can it? How can I check if Photo Album is running and run it silently from my app? Chances are that on a real iPhone/Pad it will be already running but just in case...?
My movie is in Resources of my app. I want to copy it to album. Here is the methods that I put in my AppDelegate.m to accomplish that:
-(void)downloadVideo:(NSString *)sampleMoviePath{
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(sampleMoviePath)){
UISaveVideoAtPathToSavedPhotosAlbum(sampleMoviePath, self, #selector(video:didFinishSavingWithError: contextInfo:), sampleMoviePath);
}
}
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(#"Finished with error: %#", error);
}
I call it like this:
NSString *sampleMediaPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"movie_01.m4v"];
[self downloadVideo:sampleMediaPathFromApp];
Error message that I get:
Finished saving video with error: Error Domain=ALAssetsLibraryErrorDomain Code=-3310 "Data unavailable" UserInfo=0x564ab10 {NSLocalizedRecoverySuggestion=Launch the Photos application, NSLocalizedDescription=Data unavailable}
Naturally, if I open Photo album and then run my app it works fine.
Thanks a lot for your input.
BG
----edits----
I have edited the code so it is more readable - sorry I do not have an answer, but it is a good question - I had to put this here or else I could not submit the changes. --jwk82
You can save it as..
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){

Properties of albums in ipad photo library

Is there any way to acccess the properties of the Albums synced in ipad photo library.I want to get the names with which the Album is saved in the photo library.Please anyone who have tries this or who have any knowledge in this case ,please help.
Thanks,
Christy
You'd want to use the ALAssetsLibrary. This ought to get you moving in the right direction:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
return;
//this is the album name
NSLog(#"%#",[group valueForProperty:ALAssetsGroupPropertyName]);
}
failureBlock:^(NSError *error)
{
//failure code here
}];

Delete a photo from the user's photo library?

Is there a way I can delete an image that is loaded into my app from a UIImagePickerController?
I want to be able to delete the image from the user's photo library when the user performs a specific action.
I am prompting the user to choose a image from their library, then it gets loaded into my app at which point the app does some shnazzy animation, then actually deletes the image.
Please help!
Apple doesn't actually allow you to delete from the photo library through an API. The user has to actually go to the Photos app and delete it manually themselves. Apple does allow you write to the photo library:
To save a still image to the user’s
Saved Photos album, use the
UIImageWriteToSavedPhotosAlbum
function. To save a movie to the
user’s Saved Photos album, use the
UISaveVideoAtPathToSavedPhotosAlbum
function.
But for deleting and editing/overriding an existing photo, Apple doesn't have anything like that right now.
Actually, you can delete photos saved by your app (saved to photo library with UIImageWriteToSavedPhotosAlbum API call).
The documented API [ALAsset setImageData:metadata:completionBlock:] works.
1). Add an image "photo.jpg" to your project
2). Save an image to asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
UIImage *image = [UIImage imageNamed:#"photo.jpg"];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:#{} completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Write image %# to asset library. (Error %#)", assetURL, error);
}];
3). Go to default gallery, you will find photo.jpg in your "Saved Photos" album.
4). Delete this image from asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset.isEditable) {
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Asset url %# should be deleted. (Error %#)", assetURL, error);
}];
}
}];
} failureBlock:^(NSError *error) {
}];
5). Go to default gallery, you will find photo.jpg has already been deleted.
Yes we can delete a photo. We can use PHAssetChangeRequest for this operation.
From Apple:
A request to create, delete, change metadata for, or edit the content of a Photos asset, for use in a photo library change block.
class func deleteAssets(_ assets: NSFastEnumeration)
where assets:
An array of PHAsset objects to be deleted.
PHAssetChangeRequest.deleteAssets([assetToDelete])
So, you could use the above code to delete assets.
below is swift 3 code,
PHPhotoLibrary.shared().performChanges({
let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
print(success ? "Success" : error )
})