`
xinlanzero
  • 浏览: 251823 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Download, Create and Display an Image from URL

 
阅读更多
Posted on November 4, 2009 by John Muchow in Cocoa

<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><!-- <a target="_blank" href="http://iPhoneDeveloperTips.com?wp_ct=10"><img border="0" src="/images/ads/SnowLeopard.jpg"></a><img src="http://www.assoc-amazon.com/e/ir?t=iphonedevelopertips-20&l=as2&o=1&a=B001AMPP0W" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> -->
<!-- Ad in post --><!-- <div style="float: left; width: 169; height: 76; margin:0px 0px 0px 0px;"> <a href="http://iPhoneDeveloperTips.com?wp_ct=37"><img border="0" src="/images/ads/job-board-ad-link-in-post.png"></a> </div> --><!--content with more link-->

 

This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle.

URL to Remote Image

We start by creating a URL to the remote resource:

NSURL *url = [NSURL URLWithString: 
   @"http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png"];
Create UIImage from NSData

The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
Putting it Together

Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:

NSURL *url = [NSURL URLWithString: 
   @"http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
分享到:
评论

相关推荐

    WinZip Pro 18.5 Build 11111 (x86-x64)+SerialKey

    Zip and FTP Upload Selected Files from an existing Zip file • FTP Upload new and existing Zip files • Zip and Burn new and existing Zip files directly to CD and DVD Encryption and Security • FIPS...

    Java邮件开发Fundamentals of the JavaMail API

    Instructions on how to download and install the JavaMail API are contained in the course. In addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard...

    Pro .NET 2.0 Graphics Programming

    It covers all of the classes, methods, and techniques needed to create, manipulate, and display precise graphics in a form, a page being sent to a printer, or an image. On this foundation, the ...

    javacv-platform-1.3.3-src

    JavaCV also comes with hardware accelerated full-screen image display (CanvasFrame and GLCanvasFrame), easy-to-use methods to execute code in parallel on multiple cores (Parallel), user-friendly ...

    cuteEditor6.0

    除了其强大的功能和方便的使用, 这里还有一些顶尖的技术因素是CuteEditor编辑器成为你编辑和发布Web内容的最佳选择: ... CuteEditor 5.0 lets you easily create image maps to add fun and excitement to a page...

    RxLib控件包内含RxGIF,全部源码及DEMO

    save and restore columns order and display width in ini-files and system registry; display icons for BLOB, memo, OLE and picture fields; select multiple records; convert columns headings to buttons. ...

    fastboot 2020年 android10版本 ubuntu linux平台

    Download and boot kernel from RAM. flash:raw PARTITION KERNEL [RAMDISK [SECOND]] Create boot image and flash it. --dtb DTB Specify path to DTB for boot image header version 2. --cmdline CMDLINE ...

    cocoscreator 保存图片到本地

    这通常通过调用`toDataURL`方法获取Canvas的内容作为data URL,然后创建一个新的Image对象并赋值这个URL,等待加载完成后再进行后续处理。 下面是一个简单的示例: ```javascript function canvasToImage(canvas) ...

    DIBSection Class VB

    This occurs because BitBlt doesn't completely display the bitmap during a single screen refresh and therefore the image is partially displayed before the refresh occurs. To get round this problem you...

    Senfore_DragDrop_v4.1

    7) If upgrading from a previous version of the Drag and Drop Component Suite, please read the document "upgrading_to_v4.txt" before you begin working on your existing projects. Note about "Property...

    BURNINTEST--硬件检测工具

    This problem is now just reported as an error (and BurnInTest) continues. - When BurnInTest crashes, it should now generate a "minidump" file to help debug which system component caused the failure...

    NewSID(光学习一下代码就可以了,没看清楚介绍别运行)

    This fact compromises security in Workgroup environments, and removable media security can also be compromised in networks with multiple identical computer SIDs.Demand from the Windows community has ...

    fastboot-LT18i

    flash:raw boot &lt;kernel&gt; [ &lt;ramdisk&gt; ] create bootimage and flash it devices list all connected devices reboot reboot device normally reboot-bootloader reboot device into bootloader options: -w erase ...

    Efficient MIDP Programming

    Smaller JAR file sizes can improve application performance by reducing download times and minimizing memory usage. ##### 3.1 Designing for Small Size When designing applications, consider the ...

    Using DIB Sections in VB

    Create Creates a DIB section of the specified width and height in pixels. CreateFromPicture Creates a DIB section the same size as a VB picture object and copies the bitmap in it into the DIB...

    Bochs - The cross platform IA-32 (x86) emulator

    specify an alternative redolog file of USB MSD disk image modes) - hard drive - new disk image mode 'vvfat' - ported the read-only part of Qemu's 'virtual VFAT' block driver - additions: ...

    pdf editor

    By inputting values of between 0% and 100% in Image Alpha text field, the image goes from fully transparent to fully opaque. 7. Checking for Update This option under the Help menu detects the new ...

    php.ini-development

    of the INI constants (On, Off, True, False, Yes, No and None) or an expression ; (e.g. E_ALL & ~E_NOTICE), a quoted string ("bar"), or a reference to a ; previously set variable or directive (e.g. ${...

    .htaccess

    If you do want to (this is not recommended, but possible) you should download the .htaccess file from your server first (if it exists) and then add your code to the beginning. 在使用.htaccess之前...

Global site tag (gtag.js) - Google Analytics