【小笔记】图片压缩、上传,图库操作

1、图片压缩 


  1.  
  2. 用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];  
  3. //压缩图片  
  4. - (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize  
  5. {  
  6.     // Create a graphics image context  
  7.     UIGraphicsBeginImageContext(newSize);  
  8.     // Tell the old image to draw in this newcontext, with the desired  
  9.     // new size  
  10.     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  
  11.     // Get the new image from the context  
  12.     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();  
  13.     // End the context  
  14.     UIGraphicsEndImageContext();  
  15.     // Return the new image.  
  16.     return newImage;  


2、图片上传代码

 

  1. - (IBAction)uploadButton:(id)sender {  
  2.     UIImage *image = [UIImage imageNamed:@"1.jpg"]; //图片名  
  3.     NSData *imageData = UIImageJPEGRepresentation(image,0.5);//压缩比例  
  4.     NSLog(@"字节数:%i",[imageData length]);  
  5.     // post url  
  6.     NSString *urlString = @"http://192.168.1.113:8090/text/UploadServlet";  
  7.     //服务器地址  
  8.     // setting up the request object now  
  9.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;  
  10.     [request setURL:[NSURL URLWithString:urlString]];  
  11.     [request setHTTPMethod:@"POST"];  
  12.     //  
  13.     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];  
  14.     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];  
  15.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];  
  16.     //  
  17.     NSMutableData *body = [NSMutableData data];  
  18.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
  19.     [body appendData:[[NSString stringWithString:@"Content-Disposition:form-data; name=\"userfile\"; filename=\"2.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; //上传上去的图片名字  
  20.     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
  21.     [body appendData:[NSData dataWithData:imageData]];  
  22.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
  23.     [request setHTTPBody:body];  
  24.     // NSLog(@"1-body:%@",body);  
  25.     NSLog(@"2-request:%@",request);  
  26.     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
  27.     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];  
  28.     NSLog(@"3-测试输出:%@",returnString);  
  29.     15.给imageView加载图片  
  30.     UIImage *myImage = [UIImage imageNamed:@"1.jpg"];  
  31.     [imageView setImage:myImage];  
  32.     [self.view addSubview:imageView];  

3、对图库的操作 

  1.   //选择相册:  
  2.     UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;  
  3.     if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  4.         sourceType=UIImagePickerControllerSourceTypePhotoLibrary;  
  5.     }  
  6.     UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];  
  7.     picker.delegate = self;  
  8.     picker.allowsEditing=YES;  
  9.     picker.sourceType=sourceType;  
  10.     [self presentModalViewController:picker animated:YES];  
  11.     //选择完毕:  
  12.     -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info  
  13.     {  
  14.         [picker dismissModalViewControllerAnimated:YES];  
  15.         UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];  
  16.         [self performSelector:@selector(selectPic:) withObject:imageafterDelay:0.1];  
  17.     }  
  18.     -(void)selectPic:(UIImage*)image  
  19.     {  
  20.         NSLog(@"image%@",image);  
  21.         imageView = [[UIImageView alloc] initWithImage:image];  
  22.         imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  23.         [self.viewaddSubview:imageView];  
  24.         [self performSelectorInBackground:@selector(detect:) withObject:nil];  
  25.     }  
  26.     //detect为自己定义的方法,编辑选取照片后要实现的效果  
  27.    // 取消选择:  
  28.     -(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker  
  29.     {  
  30.         [picker dismissModalViewControllerAnimated:YES];  
  31.     }  


你可能感兴趣的:(图片上传,图片压缩,图库操作)