---
title: "Creating an UIImage from a URL"
author: "xinlanzero"
date: "2011-07-22T13:36:44Z"
tags: []
categories: ["移动开发"]
slug: "creating-an-uiimage-from-a-url"
draft: false
lastmod: "2011-07-22T13:36:44Z"
id: 1130611
url: "https://xinlanzero.iteye.com/blog/1130611"
---

A question came up over the weekend as to download an image off the internet and add it to a UIImage object on the iPhone. The answer is to create an NSData object that grabs the contents of the URL and then initialize the image with that data, as such:

 

```
id path = @"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
```

&lt;!-- ISI\_LISTEN\_STOP --&gt;