I have a url of video from photo library :-
/var/mobile/Applications/9BC2EBC4-7A71-4C8B-8BFB-D25D01E4CA83/Documents/IMG_5244.MOV
How can i get convert it to NSData I am tried following but nothing worked:
NSString* fileName = movieAsset.defaultRepresentation.filename;
NSURL* fileUrl = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]URLByAppendingPathComponent:fileName];
NSString *str = [fileUrl path];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[arrVideoUrls addObject:fileUrl];
Here in your code you have add NSString as an URL and try to convert that NSString URL to NSData, so here first convert that string url to NSURL and then convert it in to NSData like below...
NSString *strVideoURL = #"assets-library://asset/asset.MOV?id=2EBD925F-D275-403E-A6A4-3487134D9B9D&ext=MOV";
NSURL *urlVideo = [NSURL URLWithString:strVideoURL];
NSData *videoData = [NSData dataWithContentsOfURL:urlvideo]];
Related
I have video data,
I need to convert the data into '.h264' format and save into a Temporary file.
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
NSURL* url = [NSURL fileURLWithPath:path];
I have created the temporary file path but, how can I attach the data along with the file and save.
Editted:
Sorry.
I was my mistake. self answering.
NSData *data = [[NSData alloc]initWithBase64EncodedString:val options:0];
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* htmlFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[data writeToFile:htmlFilePath atomically:YES];
I want to upload voice recording on server.
My file url is:
file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3
My Code is:
NSData* data = [NSData dataWithContentsOfURL:audioURL options:NSDataReadingUncached error:&error];
But the data is nil.
Please try below code
NSString *audioURLString = #"file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3";
NSString *sendStr = [[audioURLString absoluteString] stringByReplacingOccurrencesOfString:#"file:///private" withString:#""];
NSData *data = [[[NSData dataWithContentsOfFile:sendStr]]];
Good luck.....
Use this code for get data from Document folder:
if([[NSFileManager defaultManager] fileExistsAtPath:filepath)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];
}
else
{
NSLog(#"File not exits");
}
You just need to do:
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"png"];
NSString *path = [imgPath absoluteString];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:path];
Try this
NSData* data =[[NSData alloc]init];
data = [NSData dataWithContentsOfFile:[NSURL URLWithString:path]];
After run:
file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/"87C457FA-1A9F-4CA9-A651-6A3D411A0B7E"/Documents/myAudio0.mp3
The Bold text "87C457FA-1A9F-4CA9-A651-6A3D411A0B7E" may change;
You should always use the method to get the path in sandBox:
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:#"myAudio0.mp3"];
You can use this:
NSData *data = [NSData dataWithContentsOfURL:yourURL];
I am trying to place strings inside my NSURL, I have tried most suggestions but all seem to fail, i'm trying to find a easy way to do it instead of having to use some long request with long code. I'm wondering is there anything small here i'm forgetting?
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
NSURL *blogURL = [NSURL URLWithString:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
Thanks,
Curtis
+[NSURL URLWithStirng:] does not take a format string and a variable argument list. It just takes a string, so you should use something like +[NSString stringWithFormat:] first before passing it to URLWithString, like this:
NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng]];
Format the string, than add that to the NSURL
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
//Format String
NSString *urlString = [NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSURL *blogURL = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
I am unable to load an image because of nil returned by NSURL. The url looks like this : http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg. When I enter my URL on firefox, I get my image but when I try in firefox the encoder http://www.example.fr/wp-content/uploads/2014/06/pr%3Flinaire.jpg I have no image...
Here is the code I use to load my image :
NSString *myString = #"http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg";
NSURL *url = [NSURL URLWithString:myString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
That returns nil for url, so everything after is nil..
Thank you for your help guys
That's because those characters are not valid in a URL. Escape them first:
NSString *escaped = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escaped];
// etc.
How to find out and save the extension of fields that i am going to save in photo album. i want know it which format.
example:
http://www.example.com/myvideo.mp4,
http://www.example.com/mypicture.png
I need is like .mp4 and .png.If possible give me example code.
Have you looked at the documentation for NSString? You just need to send -pathExtension to your string.
If you're dealing with a string containing a URL, you should first convert it to an NSURL, then extract the path:
NSString *stringURL = #"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];
NSURL also have pathExtension (Available in iOS 4.0 and later.)
NSString *extension = [[NSURL URLWithString: #"http://sample.example.com/path/hellowwrod.ext"] pathExtension];
refer a following code.
NSString *path = #"http://www.mysite.com/myvideo.mp4";
NSString *lastPath = [path lastPathComponent];
NSString *fileExtension = [lastPath pathExtension]; // [path pathExtension];
NSLog(#"%#", lastPath); //myvideo.mp4
NSLog(#"%#", fileExtension); // mp4
For Swift:
let stringUrl = " http://www.example.com/mypicture.png"
let url = URL(string: stringUrl)
let path = url?.path
let fileExtension = url?.pathExtension