文件上传、压缩解压 操作

AFNetWork文件上传图片

<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)upload;

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];

    [sheet showInView:self.view.window];


#pragma mark - UIActionSheet

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

    // 设置代理

    ipc.delegate = self;

    

    switch (buttonIndex) {

        case 0: { // 拍照

            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;

            ipc.sourceType = UIImagePickerControllerSourceTypeCamera;

            break;

        }

        case 1: { // 相册

            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;

            ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            break;

        }

        default:

            break;

    }

    

    // 显示控制器

    [self presentViewController:ipc animated:YES completion:nil];

}


#pragma mark - UIImagePickerControllerDelegate

/**

 *  在选择完图片后调用

 *

 *  @param info   里面包含了图片信息

 */

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    // 销毁控制器

    [picker dismissViewControllerAnimated:YES completion:nil];

    

    // 获得图片

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    

    // 显示图片

    self.imageView.image = image;

}


- (void)upload1

{

    // 1.创建一个管理者

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    

    // 2.封装参数(这个字典只能放非文件参数)

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"username"] = @"123";

    params[@"age"] = @20;

    params[@"pwd"] = @"456";

    params[@"height"] = @1.55;

    

    // 2.发送一个请求

    NSString *url = @"http://upload";

    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        // 在发送请求之前会自动调用这个block

        // 需要在这个block中添加文件参数到formData

        

        /**

         FileURL : 需要上传的文件的URL路径

         name : 服务器那边接收文件用的参数名

         fileName : (告诉服务器)所上传文件的文件名

         mimeType : 所上传文件的文件类型

         */

        NSURL *url = [[NSBundle mainBundle] URLForResource:@"itcast" withExtension:@"txt"];

        [formData appendPartWithFileURL:url name:@"file" fileName:@"test.txt" mimeType:@"text/plain" error:nil];

        

        /**

         FileData : 需要上传的文件的具体数据

         name : 服务器那边接收文件用的参数名

         fileName : (告诉服务器)所上传文件的文件名

         mimeType : 所上传文件的文件类型

         */

        //        UIImage *image = [UIImage imageNamed:@"minion_01"];

        //        NSData *fileData = UIImagePNGRepresentation(image);

        //        [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"上传成功");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"上传失败");

    }];

}


- (IBAction)upload {

    if (self.imageView.image == nil) return;

    

    // 1.创建一个管理者

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    

    // 2.封装参数(这个字典只能放非文件参数)

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"username"] = @"123";

    params[@"age"] = @20;

    params[@"pwd"] = @"456";

    params[@"height"] = @1.55;

    

    // 2.发送一个请求

    NSString *url = @"http://upload";

    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);

        [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];


        // 不是用这个方法来设置文件参数

//        [formData appendPartWithFormData:fileData name:@"file"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"上传成功");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"上传失败");

    }];

    

    // 文件下载,文件比较大,断点续传技术:普遍所有的HTTP服务器都支持

    // 文件上传,文件比较大,断点续传技术:一般的HTTP服务器都不支持,常用的技术用的是SocketTCP\IPUDP

}


文件的压缩与解压

压缩

/**

 zipFile :产生的zip文件的最终路径

 directory 需要进行的压缩的文件夹路径

 */

[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:directory];


/**

 zipFile :产生的zip文件的最终路径

 files 这是一个数组,数组里面存放的是需要压缩的文件的路径

 files = @[@"/Users/apple/Destop/1.png", @"/Users/apple/Destop/3.txt"]

 */

[SSZipArchive createZipFileAtPath:zipFile withFilesAtPaths:files];


解压

/**

 zipFile :需要解压的zip文件的路径

 dest 解压到什么地方

 */

[SSZipArchive unzipFileAtPath:zipFile toDestination:dest];


SSZipArchive 依赖于lib.dylib动态库

#import "SSZipArchive.h"


    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];


    // .获得需要压缩的文件夹

    NSString *images = [caches stringByAppendingPathComponent:@"images"];

    

    // .创建一个zip文件(压缩)

    NSString *zipFile = [caches stringByAppendingPathComponent:@"images.zip"];


    BOOL result = [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];

    if(result) {

        NSString *MIMEType = [self MIMEType:[NSURL fileURLWithPath:zipFile]];

        NSData *data = [NSData dataWithContentsOfFile:zipFile];

        [self upload:@"images.zip" mimeType:MIMEType fileData:data params:@{@"username" : @"lisi"}];

    }


文件解压


NSURL *url = [NSURL URLWithString:@"http://images.zip"];

    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

//当前路径解压到指定的文件路径

        [SSZipArchive unzipFileAtPath:location.path toDestination:caches];

    }];

    [task resume];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值