taiyang2014

多张图片上传至服务器

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

posted on 2015-09-30 17:34  taiyang2014  阅读(511)  评论(0编辑  收藏  举报

导航