[英]show image from my web server to my iPhone application
i have soome image (.jpg,.png etc) stored in my web server. 我有我的网络服务器中存储的soome图像(.jpg,.png等)。 i am building an iPhone application which can upload or download images from/to my webserver.
我正在构建一个iPhone应用程序,可以从我的网络服务器上传或下载图像。
i can upload images to my web server. 我可以将图像上传到我的网络服务器。
but i can not download images from my web server to my application. 但我无法从我的网络服务器下载图像到我的应用程序。 How i can do it????
我怎么能这样做?
Simplest technique by using UIKit 使用UIKit最简单的技术
NSURL *url = [NSURL URLWithString: @"http://images.com/image.jpg"];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData: data];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
Get Image From URL 从URL获取图像
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
Save Image 保存图片
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}
Load Image 加载图片
-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
return result;
}
How-To 如何
//Definitions
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Get Image From URL
UIImage * imageFromURL = [self getImageFromURL:@"http://www.yourdomain.com/yourImage.png"];
//Save Image to Directory
[self saveImage:imageFromURL withFileName:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath];
//Load Image From Directory
UIImage * imageFromWeb = [self loadImage:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath];
For more details see these links 有关详细信息,请参阅这些链接
iOS: Download image from url and save in device iOS:从网址下载图片并保存在设备中
http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.