2012-03-10 14 views
1

カメラロールからサーバーディレクトリに画像をアップロードする方法をたくさん検索しましたが、何も見つかりませんでした。アップル(SimpleFTPSample)からのサンプルコード、と私は怒鳴るこれらのコードを取っ:FTPを使ってiOSを直接サーバーディレクトリにアップロードするには

- (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

NSStringを与え、サーバーに文字列をポストし、イメージを生成するためにWebスクリプトを介して文字列を処理する、base64にイメージをエンコードする方法はありますか?あなたのサーバに画像を保存してください – janusbalatbat

+0

サンプルコードやチュートリアルを提供できますか?ここで検索しましたが、何も見つかりませんでした!申し訳ありませんが、私は初心者です。 – Mateus

答えて

2

ダウンロードこれらlibraryに表示され、それらの画像をアップロードすることができました。

は、どのようにそれを使用するには:あなたが今、あなたのWebスクリプト内のプロセスmyImageAsStringデータ..サーバーにmyImageAsStringを投稿することができ

#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 

。もしあなたが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リクエストを作成し、このデータを含むNSStringを自分のサーバーのPHPファイルに送信し、イメージが自分のディレクトリにアップロードされた後に、 – Mateus

+0

あなたはそれに取り組まなければなりません:)私は私の答えを更新します – janusbalatbat

+0

申し訳ありませんが、私には明らかではない、私はちょうどエラーなしでクラッシュしたシミュレータを動作させるようにしました! – Mateus

1

あなたのアルバム内の画像ファイルにアクセスすることはできませんので、直接イメージファイルのNSDataのをつかむと、バイナリバッファは直接(NSInputStream inputStreamWithData [としてあなたFILESTREAMを作成することをアップロードする資産ライブラリを使用する必要があります... ]資産のNSDataのを取得する方法

iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

このスレッドで述べた他の方法は、実際には特定の状況では非常に重要であるかもしれない品質/忠実度の潜在的な損失で画像データを再圧縮が含まれます。

関連する問題