• 多张图片上传至服务器


    #pragma mark - openLibrary
    // 打开本地相册

    - (void)openLocalPhoto
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate  = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }

    - (void) imagePickerController: (UIImagePickerController*) reader
     didFinishPickingMediaWithInfo: (NSDictionary*) info
    {
        NSString *strType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([strType isEqualToString:@"public.image"]) //当选择的类型是图片
        {
            UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //先把图片转成NSData
            NSLog(@"image = %@", image);
            [reader dismissViewControllerAnimated:YES completion:nil]; //关闭相册界面
            //压缩图片
            UIImage *scalImage = [self scaleFromImage:image];
            NSString *strfileName = [self saveImage:scalImage];
            if (self.num == 1) {
                _one.image =  scalImage;
            } else if (self.num == 2) {
                _two.image = scalImage;
            } else if (self.num == 3){
                _three.image = scalImage;
            }
                   //保存到本地
        }
    }

    #pragma mark - savaImage
    // 保存图像
    - (NSString *)saveImage:(UIImage*)image
    {
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
        NSString *filePath = [docPath stringByAppendingPathComponent:@"oneImage.png"];
        BOOL flag = nil;
        BOOL rel = [fileManager fileExistsAtPath:filePath isDirectory:&flag];
        if (rel) {
            NSLog(@"存在");
            BOOL flagO = nil;
            NSString *filePathOne = [docPath stringByAppendingPathComponent:@"twoImage.png"];
            BOOL res = [fileManager fileExistsAtPath:filePathOne isDirectory:&flagO];
            if (res) {
                NSLog(@"存在");
               
                BOOL flags = nil;
                NSString *filePatht = [docPath stringByAppendingPathComponent:@"threeImage.png"];
                BOOL resu = [fileManager fileExistsAtPath:filePathOne isDirectory:&flags];
                if (resu) {
                    NSLog(@"存在");
                } else {
                    BOOL result = [data writeToFile:filePatht atomically:YES];
                    if (result) {
                        NSLog(@"写入成功");
                        self.num = 3;
                        [self.picArray addObject:filePatht];
                    }
                }
            } else {
                BOOL result = [data writeToFile:filePathOne atomically:YES];
                if (result) {
                    NSLog(@"写入成功");
                    [self.picArray addObject:filePathOne];
                    self.num = 2;
                }
            }
        } else {
            NSLog(@"不存在");
            BOOL result = [data writeToFile:filePath atomically:YES];
            [self.picArray addObject:filePath];
            if (result) {
                NSLog(@"写入成功");
                self.num = 1;
            }
        }
        return filePath;
    }
    #pragma mark - ASIHTTPRequestDelegate
    - (void)ASIFormDataRequestSuceed:(ASIFormDataRequest *)request
    {
    }

    - (void)ASIFormDataRequestFailed:(ASIFormDataRequest *)request
    {
        NSError *error = [request error];
        NSLog(@"error:%@",error);
    }
    - (IBAction)pushPic:(id)sender {
                 NSString *strUrl = [NSString stringWithFormat:@"%@", @"http://192.168.1.108:8080/resources/note/add/"];
                      strUrl = [strUrl stringByAppendingString:@"13061622013"];
                NSURL *url = [NSURL URLWithString:strUrl];
                ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
                [request setDelegate:self];
        for (int i = 0; i < self.picArray.count; i++) {
            [request addData:self.picArray[i] forKey:@"noteImg"];

        }
                [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
                [request addRequestHeader:@"Accept" value:@"application/json"];

                [request setRequestMethod:@"POST"];
       
                [request setDidFailSelector:@selector(ASIFormDataRequestFailed:)];
                [request setDidFinishSelector:@selector(ASIFormDataRequestSuceed:)];
                [request startAsynchronous];
       
    }
  • 相关阅读:
    Django的ORM
    Django model中设置多个字段联合唯一约束(多对多)
    Django 基本命令
    Django中使用Microsoft SQL SERVER
    pycharm django 再建一个app
    转载:Emmet使用手册
    线程示例(1)
    用socketserver实现简单的FTP
    python习题1-最大的不可支付邮资问题
    【实例:利用Django管理后台管理IP地址】(六)django_apscheduler实现定时检测IP使用情况
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4849775.html
Copyright © 2020-2023  润新知