如何将图像下载到应用程序中?就像我想从我的网站获取图像并将其下载到该人的 iphone 应用程序中以在应用程序中显示?基本上图像不会通过 url 显示。
更详细:
如何在不使用 UIImage 的情况下将图像下载到应用程序。我想获取图像并将其下载为文件名“anne.png”,然后使用 UIImage 作为 anne.png 在整个应用程序中引用它。看 - 我想先下载它,这样当有人第二次访问该应用程序时,他们会看到图像,同时看到默认图像.. 谢谢。?
如何将图像下载到应用程序中?就像我想从我的网站获取图像并将其下载到该人的 iphone 应用程序中以在应用程序中显示?基本上图像不会通过 url 显示。
更详细:
如何在不使用 UIImage 的情况下将图像下载到应用程序。我想获取图像并将其下载为文件名“anne.png”,然后使用 UIImage 作为 anne.png 在整个应用程序中引用它。看 - 我想先下载它,这样当有人第二次访问该应用程序时,他们会看到图像,同时看到默认图像.. 谢谢。?
对于单个图像,您可以使用以下内容。请记住,这将阻止 UI 直到图像完全下载。
[UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
要在不阻塞 UI 的情况下下载图像:
dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);
dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});
要将图像保存到手机相机胶卷,您可以使用UIImageWriteToSavedPhotosAlbum。
将图像保存在应用程序的目录中。使用 NSData 的writeToFile:atomically:
要从网站下载多张图片,也许这可以帮助您:
http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
远程图像的 URL
我们首先创建一个指向远程资源的 URL:
NSURL *url = [NSURL URLWithString: @"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
从 NSData 创建 UIImage
下一步是使用从 URL 下载的数据构建一个 UIImage,该数据由一个保存远程图像内容的 NSData 对象组成:
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
把它放在一起
以下是如何将它们包装在一起,通过从上面的 UIImage 创建 UIImageView 将远程图像作为子视图添加到现有视图:
NSURL *url = [NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];