开发者

NSURL to File Path Monotouch

I am new to iphone development. I am writing my app in Mono touch. 开发者_StackOverflow社区

I am trying to get a photo from the asset library which I can do successfully however I get the path as a URL "assets-library://asset/asset.JPG". I want to do a FileStream read which asks for a filepath. How can I convert NSURL to a file path?

I used asset.DefaultRepresentation.Url.AbsoluteString which gives the following filePath: assets-library://asset/asset.JPG?id=1000000001&ext=JPG

then when it hits the FileStream(filePath, FileMode.Open, FileAccess.Read) it gives an exception stating that Could not find part of the path.

Please Help:)


The url you get from the assets library is not a file path. It is a url to a resource in the assets library, so you cannot use FileStream to read its data.

If the asset is an image, you can get its contents to an NSData object like this:

UIImage image = UIImage.FromImage(asset.DefaultRepresentation.GetImage());
NSData data = image.AsJPEG();


You can use two different static methods to load an image. FromImage and FromBundle. The first is asynchronous. The latter is synchronous (when an image is loaded the it is cashed by iOS). Both take the path to your image.

When you use these methods, property image must be set to content property. Right click on image, property -> set build action to content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜