Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Tải lên hình ảnh từ iOS bằng ASIHTTPRequest

Tôi biết vấn đề của bạn .. Tôi đã phải đối mặt với vấn đề nhỏ .. Bạn phải cung cấp đường dẫn cho hình ảnh .. Đoạn mã sau sẽ lấy hình ảnh từ đường dẫn .. nếu hình ảnh nằm trong chỉ mục thư mục của bạn, bạn phải lấy đường dẫn hơi khác .. Hãy thử điều này và cho tôi biết ..

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"photo.jpg"];
[request setFile:[NSURL URLWithString:dataPath] forKey:@"photo"];

//IF the photo is in directory index use the following code to get the url
NSString *filename = [[NSBundle mainBundle] pathForResource:@"photo" ofType:@"png"];
[request setFile:[NSURL URLWithString:filename] forKey:@"photo"];

Ok, đây là mã bạn phải viết cho UIImagePicker

- (void) imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)imageInfo 
{
    imagePicker = nil;

    UIImage *image = [imageInfo objectForKey:@"UIImagePickerControllerEditedImage"];
    image = [image roundedCornerImage:23.5 borderSize:1];




    // Get the data for the image as a JPEG
    NSData* imageData = UIImageJPEGRepresentation(image, 0.5);

    // Give a name to the file
    NSString* imageName = @"photo.png";

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory, or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:NO];
    myPicture = imageData;
    myPicturePath = fullPathToFile;    

    // Dismissing the image picker view
    [self dismissModalViewControllerAnimated: YES];
}

Trong khi tải lên hình ảnh, hãy sử dụng mã này .. Bạn sẽ có đường dẫn myPicture được đặt từ bộ chọn hình ảnh ..

Request setFile:myPicturePath forKey:@"photo"];

Đoạn mã sau để lấy hình ảnh từ thư mục tài liệu và gửi đi ..

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString* documentsDirectory = [paths objectAtIndex:0];

                // Now we get the full path to the file
                NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"photo.png"];
                [imageData writeToFile:fullPathToFile atomically:NO];

                [Request setFile:fullPathToFile forKey:@"photo"];


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL:Nhận thông báo mới nhất từ ​​2 bảng được liên kết với nhau

  2. Ràng buộc để ngăn vi phạm ràng buộc FK trong bảng thứ ba

  3. PHP mất nhiều thời gian hơn 90 lần để chạy truy vấn so với máy khách MySQL

  4. Mysql Array không hoạt động

  5. Hàm MySQL ASIN () - Trả về Arc Sine của một số