Render Images in document.directory in a markdown file in swift(SwiftyMarkdown) - ios

Am using SwiftyMarkdown framework to render my .md file to have dynamic formatted data in my VC. But i am not able to load images in my document directory referenced in .md file. I can load images if they are available in Bundle.
Any other option or framework to fix this issue? I want to dynamically load images from server and save them documents directory and refer them in .md file which is also saved in same documents directory/folder. So the url of image and .md file is same.
"MyFile.md" in documents directory contains below content
Continue your walk, joining up with your original perimeter path of the item. "
In my VC viewdidload, i access the url path and load nsattributed string to my textview using SwiftyMarkdown frmaework
let url = self.getDocumentsDirectory().appendingPathComponent("MyFile.md")
if let md = SwiftyMarkdown(url: url) {
textView.attributedText = md.attributedString()
}
The formatted text is loaded but no image as its not available in my Bundle. But its saved in the same document directory as that of "MyFile.md"
Hope its clear!

Related

Wkwebview cannot display images in html having path of library directory

I have studied similar questions but couldnot find solution.
I am using WkWebView. It renders a html from library directory so i did loadfileurl.
It has option of displaying an image, we select the image from gallery/take camera , then from image data wecreate a file in library directory and sendthe path to web .
I tried both path var/... and also appended file://
both cases image is not displaying.
Please help.
Any suggestions appreciated.
An alternative would be to encode the images as base64 strings and insert them into the HTML before rendering. You could either save the images as images or the base64 string, depending on whether you need to use them in another context.
// Get the base64 representation of the image
let image = UIImage(named: icon) // This could also be loaded from the file system
let data = image!.pngData()
let b64String = String(data: data!.base64EncodedData(), encoding: String.Encoding.utf8)!
let finalString = "data:image/png;base64," + b64String
// Insert into HTML string before rendering in webview
let myHTML = baseHTML.replacingOccurrences(of: "#PLACEHOLDER#", with: finalString)
You will need to have #PLACEHOLDER# in your HTML file at the point where you would have the path to the image.

How do I compare 2 files with the same name in iOS to check if they are copies of the same file or different files

I am using UIDocument to save a document file to the local storage sandbox in the Documents directory. I have another file in iCloud with the same name. How would I check if those two files are copies of the same document file or two different document files with the same name?
Using NSFileVersion doesn't tell me if they are the same file, just what version of the file it is.
I tried getting the resource values of the files, but the file in the local storage sandbox gives me nil as the document identifier.
Here is code using the documentIdentifier resource value:
let localFileResourceValues = try? self.document!.fileURL.resourceValues(forKeys: [URLResourceKey.documentIdentifierKey])
let iCloudFileResourceValues = try? destinationURL.resourceValues(forKeys: [URLResourceKey.documentIdentifierKey])
print("documentIdentifier", localFileResourceValues?.documentIdentifier as Any, iCloudFileResourceValues?.documentIdentifier as Any, localFileResourceValues?.documentIdentifier == iCloudFileResourceValues?.documentIdentifier)
Print results:
documentIdentifier nil Optional(133819) false
Is it possible to get an NSMetadataItem from a file in the local storage sandbox?
I would appreciate any help.
To find out if the two file paths point to the same file object, you need to normalise the file paths by using NSString stringByStandardizingPath, then resolve symbolic links by using NSString stringByResolvingSymlinksInPath:, and then compare the paths. This web page has a good description and ready-to-go code.
To find out whether two files have the same contents, even while they might be different file objects, then NSFileManager contentsEqualAtPath:andPath: is useful.

List of files in flutter

I have images and I want to load them to my appplication.
my_app/lib/... - here my source files
my_app/assets/images/... - here my images
I need to get list of files in assets/images/ and after that show some of them (according to other logic)
I'm trying this
final dir = Directory("assets/images/");
print(dir.existsSync()); // <---- it also print: false
var files = dir.listSync().toList();
files.forEach((e) => list.add(MyImageItem(e.path)));
The problem is: I recieve exception
FileSystemException: Directory listing failed, path = 'assets/images/'
(OS Error: No such file or directory, errno = 2)
I've tried different ways: assets/images/, images/, images and so on
My pubspec.yaml
flutter:
assets:
- assets/images/
- assets/
When I create Image directly all is fine
new Image(image: AssetImage("assets/images/cat.png"))
I knew that previously (month ago) each resource has to be declared in pubspec.yaml directly, but now assets/images/ is ok.
I can load file by direct path. Why I can't access a directory? How to get list of files in directory to get them from my code?
When you add files to your assets, it means that you already know their paths.
Store all the images paths in a list and access them whenever you need.
For example if you have 3 images in
your_app/assets/images/image1.jpg
your_app/assets/images/image2.jpg
your_app/assets/images/image3.jpg
create a list and store all of them like:
List<String> imagePaths = ['assets/images/image1.jpg', 'assets/images/image2.jpg', 'assets/images/image3.jpg'];
and for example if you want to access your first image, use
AssetImage(imagePaths[0])
I keep a json file inside assets which records the file tree of assets folder.
When file list is needed, I just read from the json file.
Such json file can be easily generated by code.
However, this does not solve your problem directly when u have 5-10k images.
Only one json file might be too large to read.
Images should be grouped and recorded in separated json files.

Scanning the contents of a directory which is stored in an Xcode project in swift

This seems to be a problem which is very difficult to explain. I am trying to read data from a folder full of files and the way I am trying to do this is by scanning the whole directory for all of the files. The file in question is stored in my Xcode project in the same directory as the view controllers and storyboards. Like so:
I would like to be able to scan the contents of the "data" folder. Consider the following code:
var tmpDir = NSTemporaryDirectory()
var error: NSError?
let filesInDirectory: [String]! = fileManager.contentsOfDirectoryAtPath(tmpDir, error: &error) as? [String]
This scans the contents of the app's local folder and returns it as an array of the file names. I would like to be able to do this with the "data" folder in my Xcode project. The following code will find the contents of a single file which is stored in the same directory as the "data" folder.
let files = NSBundle.mainBundle().pathForResource("sampleFile", ofType: "csv")
var contents = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
Is there a way in which I can combine the capabilities of the two in order to scan the contents of the "data" folder in the Xcode project and then return the file names as an array?
Add the data folder to Copy Bundle Resources in Build Phases of your target.
Then you'll be able to read the path as it's within the same sandbox scope. (Assuming you are applying sandbox.) If not, just use absolute path/URL and you can access it directly without the copying process.
You might want to have a look here: Cocoa contents of directory

Cocoa/iOS: How to add a file as attachment to a PDF document?

PDF files support embedding arbitrary files as attachments (see here).
I would like to do just that in a Mac and an iPhone application using Objective-C:
Add a file as attachment to a PDF
Read the list of attached files from a PDF and extract one
Use case:
My app uses a custom document format that can only be opened by the app. I'd like to export the document as PDF and embed the original, custom document so that users who happen to have the app installed can modify the document. Everybody else can still open and print the PDF.
You can do this easily with the iOS version of the Debenu Quick PDF Library. Download the sample project and modify it with the sample code below:
How to embed a file
[DQPL LoadFromFile:path_of_my_PDF_file :#""]; //load your existing PDF file
[DQPL EmbedFile:#"Original_custom_document" :path_of_the_original_custom_document :#""]; //specify the name and the location of the attachment
[DQPL SaveToFile:new_path];
How to find the filenames of the attachments (skip if you have only one attached file)
[DQPL LoadFromFile:new_path :#""];
NSString *listItem;
int fileCount = [DQPL EmbeddedFileCount]; //returns the number of attached files
for( int count = 1; count <= fileCount; count++ ){
listItem = [DQPL GetEmbeddedFileStrProperty:count :1]; //returns the filename of the attached file
//show/read or save the returned filenames and the index of it...
}
How to extract an attached file
[DQPL LoadFromFile:new_path :#""];
[DQPL GetEmbeddedFileContentToFile:1 :path_and_filename_to_write_the_extracted_attachment]; //specify where do you want to save the extracted attachment (in this case the index of the attachment is 1)
Pal
(Debenu Team)

Resources