I am trying to read the contents of an Asset URL directly without writing to the sandbox first. If i write to the sandbox I can read the contents using
NSFileHandle *readFile = [NSFileHandle fileHandleForReadingFromURL:file error:nil];
but i am unable read an Asset URL using the same method. Although that is the right behavior as i think NSFileHandle is meant for items which are present in the user's sandbox. I was wondering if there is a way to do read from the Asset URL.
ALAsset URLs are not file URLs, so the proper way to read them is
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib assetForURL:myAssetURL resultBlock:^(ALAsset *asset) {
/* Example use. Check out the docs on ALAsset for more:
UIImage *exampleImage = [UIImage imageWithCGImage:asset.defaultRepresentation.fullResolutionImage];
*/
} failureBlock:^(NSError *error) {
NSLog(#"Failed to get asset at %#: %#",myAssetURL, error);
}];
Important: Make sure you keep the ALAssetsLibrary alive while you use the ALAsset
Related
In my new application i need to download video from different-different web sites, Say it is video downloader application. For this what i am planing is Searching html for .mp4 and .Flv url and then trying to downloading videos. There are many app already doing same
http://itunes.apple.com/in/app/video-downloader-super-lite/id481701140?mt=8
What i am asking is, how can we download video? Any code or link or something. how this application work ? Any Help will really appreciate.
what i needed is when you open a page in UIWebview Say you open "www.youtube.com" and select a video to play then it ask to download. For download i need URL (Embeded url, Flv url, mpv url) so i can paas this to function. I need to know about That URL
If you are able to use the AFNetworking library, it's pretty simple. You can make an HTTP request and use its outputStream property to download the file to your device. Say you hook up a download button to a function downloadVideoFromURL:withName:
- (void)downloadVideoFromURL:(NSURL*)url withName:(NSString*)videoName
{
//filepath to your app's documents directory
NSString *appDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videosPath = [appDocPath stringByAppendingPathComponent:#"Videos"];
NSString *filePath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4", videoName]];
//check to make sure video hasn't been downloaded already
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
//file was already downloaded
}
//video wasn't downloaded, so continue
else
{
//enable the network activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
//create a temporary filepath while downloading
NSString *tmpPath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-tmp.mp4", videoName]];
//the outputStream property is the key to downloading the file
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:tmpPath append:NO];
//if operation is completed successfully, do following
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//rename the downloaded video to its proper name
[[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:filePath error:nil];
//disable network activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
//optionally, post a notification to anyone listening that the download was successful
[[NSNotificationCenter defaultCenter] postNotificationName:#"DownloadedVideo" object:nil];
//if the operation fails, do the following:
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", error);
//delete the downloaded file (it is probably partially downloaded or corrupt)
[[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
//disable network activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
}];
//start the operation
[operation start];
}
}
if you really want to go for hacking , you you will get apple's private library, "webkit" , if you try to find the subviews of UIWebview, it might help you, i never tried it, but you can test with this logic.
I'm trying to save Images the user takes with the camera
1) If I use the UIImageWriteToSavedPhotosAlbum I can't seem to assign the fileName that I want. How, can you choose the file name?
The nice thing about using this option is
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
Then gives a thumbnail gallery of Photo Library directory.
2) which leads me to my next question Can
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; be used to get images from your own personal directory ?
3) Is there anyway to programmatically create sub folders within the Photo Library
4) Lastly,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
if (![[NSFileManager defaultManager] fileExistsAtPath:[paths objectAtIndex:0] isDirectory:&isDir]) {
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:[paths objectAtIndex:0 withIntermediateDirectories:YES attributes:nil error:&error];
}
Checking to see if the NSPicturesDirectory exists so I can write to it
I keep getting a Cocoa error 513 not permitted
Thanks in advance
You can't assign a file name to photo library images. ios assign a file name called asset url to the images that are saved to the photo library, we can't change that.
If you need to save the images to photo library you can use ALAssetsLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error)
{
// Eror
}
else
{
// Success
}
}];
[library release];
For more information check:
How to save picture to iPhone photo library?
Save image in UIImageView to iPad Photos Library
1.)
To save a filename of your choice, you need to save the image to your documents directory. This can easily be done like so:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSString *current = [NSString stringWithFormat:#"%#/%#",PHOTODATA_PATH,currentItem];
UIGraphicsBeginImageContext(CGSizeMake(160,160));
[image drawInRect:CGRectMake(0,0,160,160)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * thumbSize = UIImageJPEGRepresentation(scaledImage, 1.0);
[thumbSize writeToFile:[NSString stringWithFormat:#"%#_thumb.jpg",current] atomically:YES];
NSData * fullSize = UIImageJPEGRepresentation(image, 1.0);
[fullSize writeToFile:[NSString stringWithFormat:#"%#.jpg",current] atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
Of course this code may need to be edited to fit your exact situation but gives the basic understanding and way to save a photo to file using NSData.
2.)
Yes, UIImagePickerControllerSourceTypePhotoLibrary will access your Photo Library on your device. Any photos saved or taken from the camera roll will be accessible using this.
3.)
No, you cannot create subfolders in the Photo Library using your application. This can be done using iPhoto or iTunes or the like.
4.)
You only have access to the Documents and Library paths contained within your sandboxed environment. The only way you can save a photo to the Photo Library is by using the appropriate public methods. There is no need to check if the directory exists, the OS will take care of all of the "behind the scenes" tasks it needs to in order to manage the Photo Library.
When I try to save video by ALAssetLibrary method writeVideoAtPathToSavedPhotosAlbum the video's will be saved by name which will generated by AssetLibrary e.g. IMG_0111, IMG_0123 etc. How can I save these video's by specific name like "myAppName1.mov"
Following is the code snippet.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:tempURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
if(error)
{
}
else
{
NSLog(#"Saved video at %#\n", [assetURL path]);
}
}];
[library release], library = nil;
There is no way to achieve this at the moment. You can't specify a file name, the Photolibrary assigns as you described filenames conforming to the pattern "IMG_XXX.ext" automatically.
The only case, when the file name is preserved is when photos get imported through the camera connection kit.
Cheers,
Hendrik
I'm saving a file from a server to ALAssetLibrary so that the user can view it in offline mode
however i don't want the user to download a single file multiple times to avoid creating duplicates in the photo album application.
that's my code :
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
void (^completionBlock)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error)
{
if ( error != nil )
{
//writeFailed = YES;
NSLog(#"error = %#", error);
}
//[self notifyCompletionTarget];
NSLog(#" saving is done");
};
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
[library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:completionBlock ];
}
I think You Cant. You can use many algorithms to compare the files like SHA-1, MD5,CRC etc. However in the case of ALAsset, the mentioned all will fail.
Because , Whenever you are saving a file to Photo-Library using ALAsset, it will store the location, time,etc.. information's with the file. So the same file contend will be different corresponding to the save process .
The all above algorithms are regarding to the file contend.So they all will fail in the case of ALAsset
i have a Video file as NSData.how can i write the NSData file as Video in to Photolibrary/SavedPhotosAlbum?
i done it with UIimage through the following code. i had a UiImage as myNsData(NSData)
UIImage *viewImage = [UIImage imageWithData:myNsData];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
}
how can i do it the same functionality for Video.
i searched in NSData calss .
i found only two ways.
they are
(void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;
- (BOOL)videoAtPathIsCompatibleWithSavedPhotosAlbum:(NSURL *)videoPathURL;
two of them are using URL.actually i didn't want to write the NSData file any where except in Photolibrary/SavedPhotosAlbum.
i need to write the NSData file directly in to the Photolibrary/SavedPhotosAlbum like i saved an image.How can do that?can any one Help me?
You need to create an AVAssetWriter with a video AVAssetWriterInput and add your data into the input.
When you create the AVAssetWriter, you need to specify an URL for the asset.
After you can use the ALAssetsLibrary (writeVideoAtPathToSavedPhotosAlbum:completionBlock:) to write the asset to the photo library, with the url of the asset created above.