I am trying to figure out way to create MSStickers with images that are hosted on the web. I can create MSStickers with local images, e.g.:
NSString *imagePath = [[NSBundle mainBundle] pathForResource: #"image_name"
ofType: #"png"];
NSURL *imageURL = [NSURL fileURLWithPath: urlString];
MSSticker *sticker = [[MSSticker alloc] initWithContentsOfFileURL: stickerURL
localizedDescription: #"my-sticker"
error: &err];
But I cannot do something like this:
NSString *imageURLString = #"https://my-cdn/my-sticker.png";
NSURL *imageURL = [NSURL urlWithString: urlString];
MSSticker *sticker = [[MSSticker alloc] initWithContentsOfFileURL: stickerURL
localizedDescription: #"my-sticker"
error: &err];
No, it's not possible for the moment.
But you can do this, which is not that far from what you want:
Download the picture from your server
Store it on local directory of the device
Use the URL of this local file to create your sticker
Optional : If you don't need the image anymore, erase it from the directory
Related
I am trying to read a video file saved as a data file in the XCAssets folder of my project. I am using the following code for this -
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:#"login_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFilePath];
self.avPlayer = [AVPlayer playerWithURL: fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
This code had been working for me for quite sometime with target SDK set to iOS 8. I recently changed to iOS 9, and this now gives me a nil path and url.
The resource login_video is stored as a data file in my XCAssets folder.
What am I doing wrong here?
Thanks!
there are two way to get your url
first if you want useXCAssets
NSDataAsset *ob = [[NSDataAsset alloc]initWithName:#"a"];
NSData *obdata = ob.data;
[obdata writeToFile:#"your path" atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:#"your path"];
AVPlayerItem *player = [AVPlayerItem playerItemWithURL:filepath];
second
copy the video to mainBundle of your project and get it with
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:#"login_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFilePath];
not in XCAssets folder.
So I'm trying to use a video as a background and I have a .mov file in my app and when I run this code:
NSBundle * bundle = [NSBundle mainBundle];
NSString * urlString = [bundle pathForResource:#"movie" ofType:#"mov"];
NSURL * movieURL = [NSURL URLWithString:urlString];
if (!movieURL) {
NSLog(#"Not valid");
}
I get Not valid in the console. I checked urlString and it is giving me a url and I'm positive that the file is named correctly and is not in a directory.
So the file is there and is copied into the source. Not sure why this is doing this.
You want to use:
NSURL *moveiURL = [NSURL fileURLWithPath:urlString];
Better yet, use:
NSURL *moveURL = [bundle URLForResource:#"movie" withExtension:#"mov"];
My app downloads a pdf and then on a button press brings it up in a new view.
I get the error:
-[NSURL initFileURLWithPath:]: nil string parameter'
After some troubleshooting I pinned the problem to somewhere in this code snippet. The path that is being pointed to is in the /Documents folder where the downloaded pdf is placed. Thus the document is not in the main bundle.
NSString *path = [[NSBundle mainBundle] pathForResource:PDFpathwithextension ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Here's the download code:
//Start an NSURL connection to download from the remotepath
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:remotepathURL];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[newdata.ThirdPickerName stringByAppendingFormat:#".pdf"]];
pdfData writeToFile:filePath atomically:YES];
As NSURL is telling you, you've handed it nil instead of a valid path.
nil here means no such resource could be found by that name. Indeed, your question suggests you're well aware of this.
Since you claim your app already downloads a PDF, does it actually write that out to disk? If so, you should know where the resulting file is from doing that. If not, you first need to write the actual download code!
I need to use a NSURL object to reach different ressources on the same host.
Here is what I do:
#define MY_HOST #"my.server.eu"
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:MY_HOST path:#"/"];
Now I need to deal with
http://my.server.eu/path1
http://my.server.eu/path2
http://my.server.eu/path3
How can I modify the path of my NSURL object ?
Why can't we simply do url.path = #"path1" ?
How can I modify the path of my NSURL object ?
Why can't we simply do url.path = #"path1" ?
Because NSURL is an immutable object, and you can't change its properties afterwards. NSMutableURL does not exist, but is on the wish list of many.
In order to achieve what you're after, you're going to have to make 3 separate NSURL objects I'm afraid. To do so, you can convenience the paths within an array:
NSString *host = #"http://my.server.eu/";
NSArray *paths = #[#"path1", #"path2", #"path3"];
NSURL *path1 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[0]]];
NSURL *path2 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[1]]];
NSURL *path3 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[2]]];
You should make the base URL as you're doing and then build the others relative to it using +[NSURL URLWithString:relativeToURL:].
Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard.
Note: I only need the file URLs, I do not need to access the actual files.
You can get the URL of a file in the main bundle using
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeFile" ofType:#"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];
You can write this URL to, for example, a property list file in the Documents directory:
NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:#"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:#"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];
If you don't know the names of the files and you just want to list all the files in the bundle, use
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// do something with `url`
}
Or in Swift 4:
let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Yes, you would get their path:
NSString *path = [NSBundle mainBundle] pathForResource:#"file1" ofType:#"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]
// save it as any other object or in a dictionary:
[myMutableDictionary setObject:fileURL forKey:#"file1.png"];
EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]
There's an API on NSBundle (documentation) available on 10.6 or iOS 4 to get an NSURL directly, rather than passing a path to the NSURL constructor:
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;
It also has variants that take a subdirectory and localizationName, the same as its -pathForResource: equivalents.