2012-03-10 85 views
1

我已經搜索了很多關於如何將圖像從我的相機捲上傳到服務器目錄的文件,但我沒有找到任何東西!從蘋果(SimpleFTPSample),和我的代碼示例了波紋管這些代碼:如何使用FTP將圖像直接上傳到服務器目錄

- (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]; 
    } 
} 

這些行動進行上傳,但只是其中的圖像是我的項目中folder.So如何上傳我選擇的圖像從我的相機卷?

注意:我已經創建的UIImageView,並使其顯示從相機膠捲的圖像,作爲正常的,但如何可以上傳其顯示在UIImageView的

+0

如何將圖像編碼爲base64,這將提供一個NSString,將該字符串發佈到服務器並通過web腳本處理該字符串以生成圖像?然後將圖像保存到您的服務器 – janusbalatbat 2012-03-10 16:08:30

+0

您能否提供一些示例代碼或教程,我已經在這裏搜索,但我什麼也沒找到!對不起,我忘了提及我是初學者。 – Mateus 2012-03-10 16:46:11

回答

2

下載這些library這些圖像。

如何使用它:

#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到服務器..流程在你的網頁腳本myImageAsString數據。如果你想使用PHP,this可能會幫助你..

這是我如何管理我的web服務器中的圖像。

  <?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); 
      } 
+0

因此,我已經下載並導入了該庫,現在我需要創建一個HTTP請求並將此數據發送到我的服務器中的PHP文件,並且該圖像將上傳到我的目錄後? – Mateus 2012-03-10 17:04:14

+0

你必須努力:)我會更新我的答案 – janusbalatbat 2012-03-10 17:13:15

+0

對不起,但它並不清楚,我試圖把它的工作模擬器剛剛墜毀沒有錯誤! – Mateus 2012-03-10 17:13:26

1

因爲你不能在你的相冊訪問圖像文件直接,你必須使用資產庫來獲取圖像文件的NSData,並直接上傳二進制緩衝區(創建FILESTREAM爲[NSInputStream inputStreamWithData:... ]

如何獲得資產 iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

在這個線程提到涉及實際質量/保真度的潛在損失,這可能是在某些情況下,關鍵重新壓縮圖像數據的另一種方法的NSData的。

相關問題