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

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;
}];
}

Related

save image in camera roll and get asset url

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

ALAssetsLibrary delete ALAssetsGroup / ALAsset

I have created "photos album" from my App, using IOS AssetsLibrary.
Reading ALAssetsLibrary,ALAssetsGroup and ALAsset documentations, i have seen methods to "addAsset","addAssetsGroupAlbumWithName".
Is there a way to delete PROGRAMMATICALLY my ALAssetsGroup and ALAsset.
(the property 'editable' suppose to be TRUE because i create this data).
You can only delete the ALAsset which is created by your app with document API [ALAsset setImageData:metadata:completionBlock:] (But I have not found any API to delete a ALAssetGroup).
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.
This is not possible using any documented API. Only the photos app can delete Albums. If you want this feature to be added to iOS, I would fill a feature request at https://feedbackassistant.apple.com/.
in ios8 deleting photos might be possible using the Photos Framework
Please check the documentation of Photos Framework
For deleting assets refer to PHAssetChangeRequest
+ (void)deleteAssets:(id<NSFastEnumeration>)assets
where assets is an array of PHAsset objects to be deleted.
For deleting collections refer to PHAssetCollectionChangeRequest
+ (void)deleteAssetCollections:(id<NSFastEnumeration>)assetCollections
https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:
As Ted said, this is now possible in iOS 8 using the Photos service. It's pretty clean actually. You need to submit a change request to the photolibrary. Here's an example.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:arrayOfPHAssets];
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished deleting asset. %#", (success ? #"Success." : error));
}];
Make sure you've imported Photos, and gotten authorization from the user. (Which you probably did to show the image already)
PHAssetchangeRequest - deleteAssets
https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:
PHPhotoLibrary Class - authorizationStatus
https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/#//apple_ref/occ/clm/PHPhotoLibrary/authorizationStatus
evanchin is correct. Further more, if you want to do this in Xamarin.iOS (aka monotouch):
var lib = new ALAssetsLibrary();
lib.Enumerate(ALAssetsGroupType.All, (ALAssetsGroup group, ref bool libStop) =>
{
if (group == null)
{
return;
}
group.Enumerate((ALAsset asset, int index, ref bool groupStop) =>
{
if (asset != null && asset.Editable)
{
asset.SetImageDataAsync(new NSData(IntPtr.Zero), new NSDictionary(IntPtr.Zero));
}
});
}, error => { });
This code will delete all images that your app added to the ALAssetsLibrary.
You may delete any asset in the library using documented API ONLY.
over writing the [ALAsset isEditable] function:
#implementation ALAsset(DELETE)
-(BOOL)isEditable{
return YES;
}
#end
like evanchin said, delete the asset:
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) {
}];

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
}];

Writing metadata to ALAsset

I am developing a video app for iPhone. I am recording a video and saving it to iPhone Camera Roll using AssetsLibrary framework. The API that I have used is:
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL
completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock
Is there any way to save custom metadata of the video to the Camera Roll using ALAsset. If this is not possible using AssetsLibrary framework, can this be done using some other method. Basically I am interested in writing details about my app as a part of the video metadata.
Since iOS 4+ there is the AVFoundation framework, which also lets you read/write metadata from/to video files. There are only specific keys that you can use to add metadata using this option, but I don't believe it would be a problem.
Here's a small example that you can use to add a title to your videos (however, in this example all older metadata is removed):
// prepare metadata (add title "title")
NSMutableArray *metadata = [NSMutableArray array];
AVMutableMetadataItem *mi = [AVMutableMetadataItem metadataItem];
mi.key = AVMetadataCommonKeyTitle;
mi.keySpace = AVMetadataKeySpaceCommon;
mi.value = #"title";
[metadata addObject:mi];
// prepare video asset (SOME_URL can be an ALAsset url)
AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:SOME_URL options:nil];
// prepare to export, without transcoding if possible
AVAssetExportSession *_videoExportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetPassthrough];
[videoAsset release];
_videoExportSession.outputURL = [NSURL fileURLWithPath:_outputPath];
_videoExportSession.outputFileType = AVFileTypeQuickTimeMovie;
_videoExportSession.metadata = metadata;
[_videoExportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([_videoExportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(#"Export failed: %#", [[_videoExportSession error] localizedDescription]);
case AVAssetExportSessionStatusCancelled:
NSLog(#"Export canceled");
default:
break;
}
[_videoExportSession release]; _videoExportSession = nil;
[self finishExport]; //in finishExport you can for example call writeVideoAtPathToSavedPhotosAlbum:completionBlock: to save the video from _videoExportSession.outputURL
}];
This also shows some examples: avmetadataeditor
There is no officially supported way to do this.
What you may do: Store the info you want to save in a separate database. The downside however is that such information is then only available in your app.
What exactly are you trying to accomplish?
You can also set the metadata in the videoWriter so something like =>
NSMutableArray *metadata = [NSMutableArray array];
AVMutableMetadataItem *mi = [AVMutableMetadataItem metadataItem];
mi.key = AVMetadataCommonKeyTitle;
mi.keySpace = AVMetadataKeySpaceCommon;
mi.value = #"title";
[metadata addObject:mi];
videoWriter.metadata = metadata;
where videoWriter is of type AVAssetWriter
and then when you stop recording you call =>
[videoWriter endSessionAtSourceTime:CMTimeMake(durationInMs, 1000)];
[videoWriter finishWritingWithCompletionHandler:^() {
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib writeVideoAtPathToSavedPhotosAlbum:videoUrl
completionBlock:^(NSURL* assetURL, NSError* error) {
if (error != nil) {
NSLog( #"Video not saved");
}
}];
}];