如何直接使用FTP将图像上传到服务器目录

我已经search了很多关于如何从我的相机胶卷上载图像到服务器目录,我没有find任何东西!所以我已经从苹果(SimpleFTPSample)下载了示例代码,我把这些代码吼:

- (void)_startSend:(NSString *)filePath { BOOL success; NSURL * url; CFWriteStreamRef ftpStream; assert(filePath != nil); assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]); assert( [filePath.pathExtension isEqual:@"png"] || [filePath.pathExtension isEqual:@"jpg"] ); assert(self.networkStream == nil); // don't tap send twice in a row! assert(self.fileStream == nil); // ditto // First get and check the URL. url = [[AppDelegate sharedAppDelegate] smartURLForString:self.urlText.text]; success = (url != nil); if (success) { // Add the last part of the file name to the end of the URL to form the final // URL that we're going to put to. url = [NSMakeCollectable( CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false) ) autorelease]; success = (url != nil); } // If the URL is bogus, let the user know. Otherwise kick off the connection. if ( ! success) { self.statusLabel.text = @"Invalid URL"; } else { // Open a stream for the file we're going to send. We do not open this stream; // NSURLConnection will do it for us. self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath]; assert(self.fileStream != nil); [self.fileStream open]; // Open a CFFTPStream for the URL. ftpStream = CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url); assert(ftpStream != NULL); self.networkStream = (NSOutputStream *) ftpStream; if (self.usernameText.text.length != 0) { #pragma unused (success) //Adding this to appease the static analyzer. success = [self.networkStream setProperty:self.usernameText.text forKey:(id)kCFStreamPropertyFTPUserName]; assert(success); success = [self.networkStream setProperty:self.passwordText.text forKey:(id)kCFStreamPropertyFTPPassword]; assert(success); } self.networkStream.delegate = self; [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [self.networkStream open]; // Have to release ftpStream to balance out the create. self.networkStream // has retained this for our persistent use. CFRelease(ftpStream); // Tell the UI we're sending. [self _sendDidStart]; } } 

这些动作执行上传,但只是在我的项目文件夹内的图像。那么如何上传我从我的相机胶卷select的图像?

注意:我已经创build了一个UIImageView并使其显示来自相机胶卷的图像,但是如何上传UIImageView中显示的图像

下载这些库 。

如何使用它:

 #import "NSData+Base64.h" @implementation..... -(void)encodeImageAndSendToServer { //prepare the image NSData *imageData = UIImageJPEGRepresentation(myImageView.image, 1.0); myImageView.image = [UIImage imageWithData:imageData]; NSString *myImageAsString = [imageData base64EncodedString]; } @end 

您现在可以将myImageAsString发布到服务器..在您的web脚本中处理myImageAsString数据。 如果你想使用PHP, 这可能会帮助你..

这是我如何pipe理我的networking服务器中的图像。

  <?php $imagestring = "your posted NSString from iphone"; $file_name = 'myImage.jpg'; $img = imagecreatefromstring(base64_decode($imagestring)); if($img != false) { imagejpeg($img, '../images/comprofiler/gallery/'.$file_name); } 

由于无法直接访问相册中的图像文件,因此必须使用Assets Library来获取图像文件的NSData,并直接上传该二进制缓冲区(创build您的fileStream为[NSInputStream inputStreamWithData:…]

如何获取资产的NSData iOS:从照片库中select一个GIF,转换为NSData以用于多部分/表单数据

在这个线程中提到的另一种方法涉及实际上重新压缩图像数据,潜在的质量/保真度损失,这在某些情况下可能是至关重要的。