quicklook not showing image but has right data - ios

i'm using quicklook to preview an image saved locally. when i run the app, quicklook read the right file and has the right image memory but doesn't preview it. it shows me something like this.
in my code i'm passing an NSURL to this delegate method
(id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
so i guess everything is in place. but what could i missing?
EDIT:
to creath the path i do it by the following:
filePath = [NSString stringWithFormat:#"%#/LeBaccPictureDownloadedTemperoraly", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
and in the delegate method i mentioned i return the url by this:
[NSURL fileURLWithPath:filePath];

does filePath = [#"~/Documents/LeBaccPictureDownloadedTemperoraly" stringByExpandingTildeInPath]; change anything? Does the file have an extension? Try to show the file in an UIImageView to make sure the file is not broken. Whatch the file format xD
Like this?

Related

Get URL to a PDF thats not in documents directory - UIDocumentInteractionController

I have a PDF that is dynamically generated from saved core data every time, rather than stored in the documents directory as NSData self.pdfData, rendered in a UIWebView. Rather than using UIActivityController to share the PDF i'd like to use UIDocumentInteractionController to get a full range of sharing options. The issue is this only seems to be for saved PDF's in the documents directory.
NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:#"sample" withExtension:#"pdf"];
Id rather not save it to documents directory then delete it, I was hoping for something more elegant. I tried to get the URL directly from the UIWebview of the PDF using self.webView.request.mainDocumentURL however this returns about:blank
NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:#"sample" withExtension:#"pdf"]; <-----
if (PDFUrl) {
DebugLog(#"Loading document");
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:PDFUrl];
[self.documentController setDelegate:self];
[self.documentController presentPreviewAnimated:YES];
}
UIDocumentInteractionController only works with a file on the local file system. This means you must save the PDF to a file before you can use UIDocumentInteractionController.
Write the PDF data to a file in the caches folder (NSCachesDirectory), get its file URL, use the UIDocumentInteractionController, and then delete the file when complete.
Or use UIActivityViewController and pass the NSData of the PDF as the activity item.

Local saved videos don't play again after app closes

This is strange, but basically I download and save a video locally, and the store the url path to provide to an AVPlayer to play.
This works fine the first time I do it. I download a file, and then I can play it to my hearts content as many times UNTIL I exit the app. When I launch the app a second time, I now get a black screen when I try to play the same exact video using the same exact path.
Because I am using the Simulator I can verify that the videos and pictures indeed very much still exist in the same folder I saved them to, and I can still play them if I click on them from the Finder.
Maybe it's a caching issue? If it matters, I've saved them straight to the Library directory as I test this.
Relevant Code:
NSString *outputFile = [NSString stringWithFormat:#"video_%#.mp4", guid];
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
// save the video to the URL
Then I "persist" it using an NSString [fileURL path] (The way I've built this out, assume the solution requires an NSString to NSURL conversion).
Later I create an AVPlayerItem:
NSURL *url = [NSURL fileURLWithPath:persistedObject.contentURL];
NSLog(#"url: %#", url); // prints a valid location**
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
** for example this is a sample url location
url: file:///Users/gabriel/Library/Developer/CoreSimulator/Devices/CE1FC933-808C-4003-9BE4-DEC59B787FF7/data/Containers/Data/Application/FAD072B4-B5B0-4487-8A76-57B047324A00/Library/picture_D8DEAFA5-0843-4AA3-BB32-C61E32D13579.mp4
It's been suggested I use URLForDirectory:inDomain:appropriateForURL:create:error: and URLByAppendingPathComponent: instead, which I will look into. But still confused as to why it would play when I first download it, but not after app exits when it's the same exact file.
You've made a classic mistake. You are persisting the full path. But the full path changes. Never persist a full path. Only persist the part of the path relative to the value obtained from NSSearchPathForDirectoriesInDomains.
Given what you are doing, you should only persist the base filename (outputFile). Then when the app starts, you rebuild the full path again like you did originally but use the persisted filename to append it to the dynamically obtained path to the application support folder.

xcode save image to a specific file in library

I would like to save an image to the Assets library with a specific name. Or to the Photos library. I think it's the same thing. I would like to be able to save the file like "file1.png". So far I tried with writeToFile method but I couldn't get the path right.
Hope this helps you:
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.png"]; // identity the home directory and file name
[data writeToFile:path atomically:YES];
NSString *imageFile = [#"file://" stringByAppendingString:path];
NSLog(#"----imageFile path--%#",imageFile);

iOS - Reading an Audio file from Documents Directory

I am saving audio data to the Documents directory and trying to read it back. If I play it back immediately it plays successfully, however, if I start a new session and try and play the song locally it will fail even though listing the files in the Documents directory shows that my file is still there. Note that the file is played back from the Documents folder in the same way (same code) if it is played immediately or during a new session.
Here is how I save the audio data to the Documents directory:
+(void)writeDataToAudioFile:(NSData*)data forTrack:(MediaItem*)track
{
// filename looks like "[track_id].mp3"
NSString *fileName = [NSString stringWithFormat:#"%#.%#",track.sc_id,track.original_format];
NSString *pathName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) firstObject]
stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:pathName
contents:data
attributes:nil];
}
Then in my music player I want to load the local URL to this file to initialize the AVPlayer:
NSURL *url;
if(_currentTrack.is_local_item)
{
url = [NSURL fileURLWithPath:_currentTrack.local_file_path];
}
url does not get created properly as AVPlayer does not play. Furthermore, I have tried every different way to load the file as data into an NSData object to check the byte size but trying to access the file as data always returns nil. However, the file exists as if I use NSFileManager I am able to iterate over the items in the Documents directory and print their file names/paths, validating that I the path I have saved in "_currentTrack.local_file_path" does exist. Again, if I play the file immediately after saving the file to disk it will play back.
If there is more info I can provide to make this clearer I will. Thank you very much.
Do not write the full directory path to DB. It will change. You need to only save the file name to DB as reference. Then use as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *fileName = #"SAVED_FILE_NAME.mp3"; // eg: [track_id].mp3
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
This will provide you the actual path of the file.
Keep coding........... :)
I found the solution after putting the problem down for a few days. I break-pointed and print-stated the heck out of the program and I found that the file path I was saving was not the same as the file path of the file.
I think this was a simulator issue, as the issue only occurred between different sessions of the simulator, and worked within the same session, so the device id (which is part of the absolute path) was changing - maybe someone more knowledgeable can weigh in on that.
Pay closer attention to the string values of your variables folks!

How can I open an image from file system into UIImage View

Before anyone down votes my question, I have literally looked all over stack and cannot find the answer
I am making a phonegap app which I can place an image into my filesystem
the url :
file:///Users/danielnasello/Library/Application Support/iPhone Simulator/7.0.3/Applications/75EE3563-560D-4CFD-B357-313DD559573D/Documents/Vault/1386252707450.jpg
I can then pass this url to my modal controller to present an image in the image view.
the problem is I cannot seem to find the correct way to access the image from my filesystem and display it into my image view.
my current relevant code
(self.myImage) is the string i pass from phonegap. it does contain the url string because I am logging it, so I know the url is getting passed. However, the image simply will not display.
I tried using image named from one of my library images and it works fine. I just cant seem to find the correct way to present it using file url.
NSURL *url = [NSURL URLWithString:self.myImage];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
self.imageView.image= img;
here is the code for that.
NSString *filepath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"1386252707450.jpg"];
self.imageView.image=[UIImage imageWithContentsOfFile:filepath];
this code is actually correct. It just doesn't work on simulators (shocker), you need a real device

Resources