1

Is it possible to load an image asset from CoreGraphics using MonoTouch?

If so, how?

I can't find anything in the MonoTouch framework that will let me do this.

FYI, I am looking to do this with CoreGraphics, not with CoreImage. I want my app to be compatible with iOS4+, so any way to do it with this in mind is acceptable.

FYI, I already have the asset object I need. I just need to turn this into something the user can see on the screen.

poupou
  • 43,413
  • 6
  • 77
  • 174
user1060500
  • 1,505
  • 1
  • 21
  • 40
  • What do you mean by "loading an asset from CoreGraphics"? What kind of assets? From where? –  Jan 22 '13 at 18:06
  • An image stored in the Saved Photos media folder which I already have the Asset url for. – user1060500 Jan 22 '13 at 18:10
  • @userXXX http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html –  Jan 22 '13 at 18:11
  • Easy. Obviously. I already have the Asset. I need to turn this into an Image object which I can add to the view so the user can see it. I already know how to get the asset object itself and I have done this. That was the easy part. In my first comment I indicated I already had the asset url. – user1060500 Jan 22 '13 at 18:15
  • @userXXX Oh, I see, sorry. In this case, [this](http://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone) might get you started. –  Jan 22 '13 at 18:19

1 Answers1

1

Here's how to load a .PNG into a CGImage using a CGDataProvider and then initialize an UIImage from it (similar to your previous questions about CoreImage).

string file = Path.Combine (NSBundle.MainBundle.ResourcePath, "image.png");
using (var dp = new CGDataProvider (file))
using (var img = CGImage.FromPNG (dp, null, false, CGColorRenderingIntent.Default))
using (var ui = new UIImage (img, 1.0f, UIImageOrientation.Up)) {
    Assert.IsNotNull (ui.CGImage, "CGImage");
}

You can use the FromJPEG method if you have JPEG images (or adjust automatically based on your URL file extension).

poupou
  • 43,413
  • 6
  • 77
  • 174
  • This will load a file from a resource. I need to load from an Asset URL. – user1060500 Jan 22 '13 at 18:33
  • Wrong, it will load an image from the URL. The URL is constructed from the MainBundle's resource location - but the `CGDataProvider` is **not** aware of this (e.g. it could come from a web server across the planet, not from the device itself). – poupou Jan 22 '13 at 18:35
  • So, you are saying to use the Asset URL such as this and use the rest of your code and it should work? string file = ("assets-library://asset/asset.JPG?id=CF72ABDE-76CE-4411-AB7D-587B3540A140&ext=JPG"); using (var dp = new CGDataProvider (file)) -this does not work for me. On creating a CGDataProvider I get an ArgumentException. :( – user1060500 Jan 22 '13 at 20:21
  • Not for that custom scheme (it's internal and was introduced, in the newer SDK, not to give the *real* location). You would have saved much time by giving more code (how you got the URL) or the URL itself. To get your image you need to get the `ALAssetRepresentation` for the `ALAsset` and ask for its fullscreen image. E.g. `CGImage img = asset.DefaultRepresentation.GetImage();` – poupou Jan 22 '13 at 20:46
  • I mentioned I had an Asset object for an internal photo from the Saved Photos media folder. Is this backwards compatible with iOS5/4, etc? – user1060500 Jan 22 '13 at 21:24
  • Apple documents them as available since iOS 4.0. – poupou Jan 22 '13 at 21:37