Load Images from Web with Xamarin iOS EN
Hi Everybody, today I´m going to show you how to load images from the web in Xamarin in an iOS project, to do this first we need to follow these steps:
- Create a New Solution of Single view in Xamarin, if you don´t know how to do it please click here to see how to do it in a previous post
- Now edit your .xib file to add an Image View and three UIButtons
- Now let´s link our objects with their variables, when you have done this you can close your xCode
- Now in Xamarin go to [Name of your project].cs file create the following method:
UIImage FromUrl (string uri)
{
using (var url = new NSUrl (uri))
using (var data = NSData.FromUrl (url))
return UIImage.LoadFromData (data);
}
{
using (var url = new NSUrl (uri))
using (var data = NSData.FromUrl (url))
return UIImage.LoadFromData (data);
}
- Now on your [Name of your project].designer.cs file and add the following code on your respective areas, I'm going to paste all my class so in that way you can see how to implement this code
using MonoTouch.Foundation;
using System.CodeDom.Compiler;
namespace ImageFromWeb
{
[Register ("ImageFromWebViewController")]
partial class ImageFromWebViewController
{
[Outlet]
MonoTouch.UIKit.UIImageView imgImage { get; set; }
[Action ("btnImg1:")]
partial void btnImg1 (MonoTouch.Foundation.NSObject sender);
partial void btnImg1 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://bilmoore.com/wp-content/uploads/2012/01/friends-tv-show-wallpapers-1280x1024.jpg");
}
[Action ("btnImg2:")]
partial void btnImg2 (MonoTouch.Foundation.NSObject sender);
partial void btnImg2 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://www.8music.es/wp-content/uploads/2012/01/nickelback-7016.jpg");
}
[Action ("btnImg3:")]
partial void btnImg3 (MonoTouch.Foundation.NSObject sender);
partial void btnImg3 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://www.orgullobianconero.vavel.com/wp-content/uploads/2013/03/Il-futuro-di-Del-Piero.jpg");
}
void ReleaseDesignerOutlets ()
{
if (imgImage != null) {
imgImage.Dispose ();
imgImage = null;
}
}
}
}
using System.CodeDom.Compiler;
namespace ImageFromWeb
{
[Register ("ImageFromWebViewController")]
partial class ImageFromWebViewController
{
[Outlet]
MonoTouch.UIKit.UIImageView imgImage { get; set; }
[Action ("btnImg1:")]
partial void btnImg1 (MonoTouch.Foundation.NSObject sender);
partial void btnImg1 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://bilmoore.com/wp-content/uploads/2012/01/friends-tv-show-wallpapers-1280x1024.jpg");
}
[Action ("btnImg2:")]
partial void btnImg2 (MonoTouch.Foundation.NSObject sender);
partial void btnImg2 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://www.8music.es/wp-content/uploads/2012/01/nickelback-7016.jpg");
}
[Action ("btnImg3:")]
partial void btnImg3 (MonoTouch.Foundation.NSObject sender);
partial void btnImg3 (MonoTouch.Foundation.NSObject sender){
imgImage.Image=this.FromUrl("http://www.orgullobianconero.vavel.com/wp-content/uploads/2013/03/Il-futuro-di-Del-Piero.jpg");
}
void ReleaseDesignerOutlets ()
{
if (imgImage != null) {
imgImage.Dispose ();
imgImage = null;
}
}
}
}
- Now after that you just need to run your app (If you have copied and pasted this code you will see that I'm a huge fan of Friends, Nickelback and Alessandro del Piero =P)
If you have problems following the steps, you can watch the next video to see how to do it:
Well guys that's it for today, you can see my entire project on GitHub clicking here and if you have questions or doubts you can leave them on the respective area, see you ^_^/
Well guys that's it for today, you can see my entire project on GitHub clicking here and if you have questions or doubts you can leave them on the respective area, see you ^_^/
This an informative and helpful post - so clear and easy to follow step by step process of how to load images from the web in Xamarin in an iOS project.
ReplyDeleteXamarin Consultants.