Giter Site home page Giter Site logo

Comments (11)

cjwirth avatar cjwirth commented on May 16, 2024

Hey!

Sorry for taking so long to respond to this. I went and tried it out using this code:

let image = RichEditorOptionItem(image: nil, title: "Add Image") { toolbar in
    let manager = NSFileManager.defaultManager()
    let documents = manager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
    let filepath = documents.URLByAppendingPathComponent("pasta.jpg", isDirectory: false)

    if !manager.fileExistsAtPath(filepath.path!) {
        let url = NSURL(string: "http://www.crissa.ca/images/Pasta-Dish.jpg")!
        let data = NSData(contentsOfURL: url)!
        _ = try! manager.createDirectoryAtURL(documents, withIntermediateDirectories: true, attributes: nil)
        data.writeToURL(filepath, atomically: true)
    }

    toolbar?.editor!.insertImage(filepath.absoluteString, alt: "Our Special Image")
}

This way the first time I run it, it downloads the image, and after that first time, it just loads it straight from the local Documents directory.

This code is working fine for me, so I'm not really sure what your problem is. It looks like you're using one of the sample images from the Photo Library? I have no idea if there is a connection, but maybe you are having some permissions problem the 2nd time running, because you haven't opened the Photo Library assets, but are trying to use the URL directly? I'm not sure if it works like that though.

Do you have any sample code that reproduces this problem? I'd like to take a look and see what's going on.

from richeditorview.

DominikButz avatar DominikButz commented on May 16, 2024

Thanks for looking into this! The code that saves the image to the Documents folder of the app is very similar to yours (see below). I also tried using safari in the simulator to save some random image to the simulator camera roll rather than using one of the default images. The result is the same...
In the logic of this app however, the user cannot just insert an image by adding a web URL. It all works through the camera or the image library.

After dismissing the edit view and reopening the same item ( a note object in this case), the image appears as part of the Html. but I guess it is kept in cache so the simulator does not actually reload it from the documents folder...
When I stop the simulator and rebuild however, the web view in the editor apparently can't reload the image...

text information in the html-string can be retrieved without any problem though - it is just the image that is missing.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        if let imageData = pickedImage.resizeImage(200.0 , maxWidth:300.0, compression:0.7) {
            let filename = NSUUID().UUIDString + ".jpg"
            let path = getDocumentsURL()
            let fileURL = path.URLByAppendingPathComponent(filename)
            print("file url: \(fileURL)")
            do {
                try imageData.writeToURL(fileURL, options: NSDataWritingOptions.DataWritingAtomic)
            }
            catch let error as NSError  {
                print(error)
            }
            self.keyboardToolbar.editor?.insertImage(fileURL.absoluteString, alt: "new image")
            dismissViewControllerAnimated(true, completion: {
                print("file url abs string \(fileURL.absoluteString)")
            })
        }
    } else {
        dismissViewControllerAnimated(true, completion: nil)
    }
}

func getDocumentsURL() -> NSURL {
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    return documentsURL
}

from richeditorview.

cjwirth avatar cjwirth commented on May 16, 2024

Ah, I see. You're actually saving the local image URL in the HTML, and then displaying it again the next time with the same HTML.

I played around with it again today. It seems like the Documents directory URL is changing each time you launch the app. For example, for one run, I got

file:/// ... /Application/6D820E65-30F6-480A-90FE-C31B0CDB6F65/Documents/pasta.jpg

But then the next time I got

file:/// ... /Application/6399E27A-E729-46D6-A523-45FD2D65CFBA/Documents/pasta.jpg

So it worked for my sample code, because I am always generating the documents directory code before inserting it into the web view.

For you, it's a little bit more difficult. I might suggest, when you save the HTML, you take the documents directory, and change it to some token, like [DOCUMENTS_DIRECTORY]/pasta.jpg . Then, when you load the content the next time, you pre-process the HTML, changing [DOCUMENTS_DIRECTORY] with whatever the real new documents directory is.

How does that sound, does that fit your situation?

from richeditorview.

cjwirth avatar cjwirth commented on May 16, 2024

I think I'm going to close this issue, because I don't think it's really a problem with the editor itself. I don't think local URLs like that were supported in the first place (or else why would they change the directory? lol)

If you think differentl or something, feel free to reopen the issue 👍

from richeditorview.

DominikButz avatar DominikButz commented on May 16, 2024

Thank you for figuring this out! I have opened the sub folder in the core simulator path and indeed the folder name changes every time I do a new build.
This is actually an issue that only occurs in the simulator. I have just tested my app on a real device for the first time. When I add an image to a note, save it and then simply remove the app from memory and re-start it, the image is displayed as expected. Thanks anyway and yes, you can close they issue!

from richeditorview.

NancyZY avatar NancyZY commented on May 16, 2024

@DominikButz Can you get the resizeImage() method in there ? I try to pick image from Photo Library, and then set image to "toolbar.editor", there I found you have worded that . So I copied your code ,then showed error in resizeImage() method . Need your help, Thx

from richeditorview.

cjwirth avatar cjwirth commented on May 16, 2024

You won't be able to just copy/paste it, but I'm pretty sure he's using an extension on UIImage that has a resizeImage method that looks something like what is in the SO answer

from richeditorview.

DominikButz avatar DominikButz commented on May 16, 2024

yes, that is correct, I used an extension to UIImage. there are several ways of resizing images. you can find a lot of solutions on numerous sites.

here is the one I used:

public extension UIImage {

    public func resizeImage(maxHeight:CGFloat, maxWidth:CGFloat, compression:CGFloat)->NSData? {

        var actualHeight = self.size.height
        var actualWidth = self.size.width
        var imgRatio = actualWidth/actualHeight
        let maxRatio = maxWidth/maxHeight

        if actualHeight > maxHeight || actualWidth > maxWidth
        {
            if imgRatio < maxRatio
            {
                //adjust width according to maxHeight
                imgRatio = maxHeight / actualHeight
                actualWidth = imgRatio * actualWidth
                actualHeight = maxHeight
            }
            else if(imgRatio > maxRatio)
            {
                //adjust height according to maxWidth
                imgRatio = maxWidth / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = maxWidth;
            }
            else
            {
                actualHeight = maxHeight;
                actualWidth = maxWidth;
            }
        }

        let rect = CGRectMake(0, 0, actualWidth, actualHeight)
        UIGraphicsBeginImageContext(rect.size)
        self.drawInRect(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        let imageData = UIImageJPEGRepresentation(image, compression)
        UIGraphicsEndImageContext()

        return imageData! as NSData

    }


}

from richeditorview.

NancyZY avatar NancyZY commented on May 16, 2024

So kind of you, I just wonder why I insert image does not work, I made the class achieve UIImagePickerControllerDelegate , there in
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) like the DominikButz's code , then there no image insert, I try to print some information , the console has no hint. I cannot find the result

from richeditorview.

cjwirth avatar cjwirth commented on May 16, 2024

Did you set you class to be the image picker's delegate? Is the delegate method actually being called?

from richeditorview.

DominikButz avatar DominikButz commented on May 16, 2024

yes, you need to create an instance variable like

let imagePicker = UIImagePickerController()

first and in viewDidLoad :

imagePicker.delegate  = self

use print(...) and check in the console if the function didFinishPickingMediaWithInfo is called.

from richeditorview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.