Storing an NSImage in a Core Data Model - cocoa

What is the correct way to store an NSImage in a Core Data Model? I have assumed adding an Attribute to an Entity and giving it the Type "Binary" should work, but it is not working. I have a table with a column of NSImageCells and it is not showing anything.

If you can work in 10.5+, the easiest way is to store the NSImageReps for the image in "Transformable" attribute. By default, the transformable attributes use NSCoding to archive their values on set and unarchive on access. This saves you from having to write custom getters/setters. If you want to get fancy, you could write a custom NSValueTransformer that converts an image to an acrhived version of its representations and visa versa on get. If you're using 10.4, then you have to write custom getters/setters (see Apple's docs on creating Non-standard persistent attributes. You can get the image's image reps by sending the NSImage a -representations message.
If you want to display the images in a UI via bindings, you should also read the Displaying Images Using Bindings section of the Cocoa Bindings Programming Topics.

This doesn't answer the exact question you asked, but depending on how many images you are storing it can be more efficient to store only paths or URLs to the images, saved in your own location, and load them as required.

You'll need to create an NSData representation of it.
Have a look at Non-Standard Persistent Attributes. Especially the section under Transformable Attributes, and Custom Code if that doesn't sort you out.

Here is a really easy implementation
http://objectivesheep.com/blog/nsimage_coredata/

Related

How does ImageSource return bytes to the application

I would like to implement my own subclass of ImageSource. Xamarin Forms has FileImageSource, UriImageSource and several other subclasses of ImageSource, but none of them meets my needs.
To see how to implement an ImageSource subclass, I looked at implementation of FileImageSource on github (https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Core/FileImageSource.cs)
I expected to find that the subclass would override one or more ImageSource methods to actually read the image file and provide the bytes from the file to the calling platform. But source code of FileImageSource contains no such thing! In fact, they use the File only to override ImageSource.IsEmpty. How can this be? If an image is contained in a file, you need to read the file to display this image, don't you?
What am I missing? And if I want to implement my own subclass of ImageSource, how can my sublass return image data to the platform?
Edit
Thank you to everyone who proposed solving the issue in a different way and inquiring why exactly existing ImageSource subclasses do not serve my needs.
Even if there is a different way to solve my immediate issue, from an educational perspective it is still interesting to know how Xamarin Forms displays an image file without reading the file. The source code for FileImageSource is quite short and does not appear to use the file, other than to implement IsEmpty. This will bug me until I know how this works.
[Resolve the issue using converters]
Instead of creating a new ImageSource, you could create a Converter (if you are working with bindings).
So if you have something like a byte array, and you want that to be shown as an image, you can do the conversion from byte array into a StreamImageSource
[EDIT: Knowing how it works]
If you want to know how it works internally and how it transforms the File into the actual Image, you will have to go deeper into the code, and check the platform implementations.
For example here you have the android renderers. You will find the IImageSourceHandler.cs and the FontImageSourceHandler and the FileImageSourceHandler and so on.

Image dimensions metadata on google-cloud-storage.

I need to know the dimensions of images I'm saving, so that I can add them to the og:image:width and og:image:height meta tags for image previewing by Facebook, Twitter etc - as is suggested by this Facebook documentation.
If I'm saving these images to a google storage bucket - is there any already existing way of finding these images sizes with existing standard metadata, or will I need to add some custom metadata as described in this google documentation?
Given that Cloud Storage sees everything as objects, it does not have multiple object definitions, depending on the type of the object (like media, text... it does something like this via Content-Type but it's not what you're looking for). In this sense, there are no object type characteristic-metadata (i.e. width and height of images or length for sounds or videos).
The only predefined (or how you call it, standard) metadata entries are the ones mentioned here and, yes, as you specified, you need to add custom metadata.

MvxImageViewLoader behaviour overriding (Caching, Images appearing etc.)

I use MvxImageViewLoader for MvvmCross Xamarin applications.
This component is really great and simplifies the images loading pretty much, but it (at least, out-of-the-box) is pretty basic and most of modern applications require some reacher functionality, for instance, loading/progress images or images nice appearing/transitions (possibly with custom animation).
I see there is DefaultImagePath property, but that's static image, which I can probably use by default, but that's not animated view or something.
So, is there any way to customize/extend the loader behaviour (for default image appearing, images transition (from default to loaded)) etc?
And also I've noticed that the loader caches the image and even if I trigger bound property changing (leaving the image url the same) it does not refresh the image. I guess, "caching" really means caching and so on, but what if I need to change the user icon or something... how can I forse the cache refreshing with the image loader?
Thank you!
So, is there any way to customize/extend the loader behaviour (for default image appearing, images transition (from default to loaded)) etc?
No - advanced features like fade-in/fade-out/animated-placeholder display aren't supported within the standard MvvmCross image view, and no-one that I know of has provided any samples or tutorials about how this can be done.
For adding such functionality, you can use normal software techniques - inheritance, aggregation and cut, copy, paste. e.g. you could simply create your own AgatImageView which had the behaviour your app requires based on MvxImageView.cs.
Some examples of creating your own data-bound controls is given in N=18 and N-19 of http://mvvmcross.wordpress.com/
As you already mentioned in upper comments you may use default iOS' UIActivityIndicatorView for showing progress and you should hide the progress in afterImageChangeAction, you can check if UIImageView.Image field is not null, to make sure that the image is loaded.
Regarding the caching, it's not that easy here. By default MvxImageViewLoader relies on MvvmCross framework's implementation of IMvxFileDownloadCache interface. This interface has only one public method RequestLocalFilePath(), so even if you get an instance from IoC container (Mvx.Resolve()) you won't be able to clean-up the existing cache (to do that you need to reset private _entriesByHttpUrl field of MvxFileDownloadCache class).
If you really need this, you have to copy-paste existing MvxFileDownloadCache class and make your tweaks. But I am not sure about your use-cases where you need this. If you download images from the web, the URL of the image is a sort of a key in the cache, so if you need to reload just change the URL.
Maybe you could use some old-school approach like adding GET parameters to the URL: http://mydomain.com/images/myimage.jpg?timestamp=123456. Usually this helps everywhere :-). Although I didn't test it with MvxImageViewLoader, it's just my best guess.

Core Data and image entities

I'm new to Core Data. I'm creating an app that will allow a user to add a document importing/exporting from/to either Dropbox, Mail (haven't figured that out), or the camera or photo library.
I see in CoreDataRecipes that Apple creating an entity for image and have a relationship to recipes. And their PhotoPicker app the camera part is a little ridiculous in my opinion. Cool but not practical. Plus they're not using Core Data.
I just have a table view, an add button at the top, the user will be prompted to choose any of the options mentioned, add a title, and that title added to the table view. Hope all that is clear, I'm trying to be brief. In a way I want to do this part of the app like a scanner app.
So my question is, do I really need to create an entity for the image? Or can I just go about figuring how to do all this?
Thanks in advance for any help or info you can provide.
In my case, I kept the images out of sql store altogether and just saved the images in the application's /Cache folder. I then stored a filename in my entity in core data which allowed me to access the image.
Below 1MB do with it whatever you like. Beyond 1MB you should put the binary data on a separate table. What you get doing this is lazy loading, that is, the data remains at fault until the user needs it. It also depends on your memory usage and number of images, this part is only common sense.

Cocoa: Creating a Custom Text View

In a nutshell, I don't want raw text, or even rich text. I want to load an xml document, which has metadata for sections of text, and I want to display that metadata in a drawer when I click on a given text section. A hyperlink is a good example; obviously trivial to do in a web app, but while I'm not that experienced with mac dev, I can't seem to find an easy way to accomplish this with cocoa.
Any suggestions as to general strategy? There doesn't seem to be an HTML view built in to Interface builder or I'd mess with that.
I'm not entirely clear on what you're trying to do. It sounds like you want to load an XML document, display the text, and display various metadata when certain bits of text are selected.
If that's the case, you should read about the Cocoa Text System. The NSTextStorage class is a subclass of NSMutableAttributedString, and you can apply arbitrary attributes to any range of text. When the selection changes, you can get the attributes in the selected range and use that to update your drawer. (By the way, drawers are really on their way out. I'd suggest a different user interface. NSSplitView-based interfaces are much more in vogue these days.)
Of course, to build up the NSTextStorage, you'd need to parse the XML with NSXMLDocument or NSXMLParser, but you'd get much more control and it would look more "Cocoa-like".
You could use a WebView, which is the Safari renderer, but I think you'd have a hard time getting it to display text the way you want. Safari has never been great at rendering XML without XSLT.

Resources