简体   繁体   English

将图像从我的Web服务器显示到我的iPhone应用程序

[英]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.

相关问题 如何在我的iPhone应用程序中从服务器端显示推送通知 - How to show Push Notification from server side in my iPhone application 在 Facebook 中显示我的 iphone 应用程序 - Show my iphone application in Facebook 如何在iPhone应用程序中将我的UItextfield值插入到MySQL Web服务器? - How to insert my UItextfield value to mysql web server in iphone application? 图像 Slider 到我的 iPhone 应用程序 - Image Slider to my iPhone application 如何使我的iPhone应用程序将我的Facebook身份验证令牌传递到Web服务器? - How do I make my iPhone application pass in my Facebook auth token to my web server? iPhone无法从我的网页(服务器)播放视频 - iPhone can not play video from my web page (server) 如何将音频文件从服务器下载到我的 iPhone 应用程序中? - How to download audio files from a server into my iPhone application? 我公司应用程序服务器的iPhone日历推送(JAVA环境) - Iphone calendar push from my company application server(JAVA Environment) 在iPhone的共享对话框中显示我自己的应用程序 - Show my own application in share dialog in iPhone 如何在我的iPhone应用程序中显示* .mpp文件? - how to show *.mpp files in my iphone application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM