IOS 图片上传处理 图片压缩 图片处理

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePickerController类来和用户交互。

使用UIImagePickerController和用户交互,我们需要实现2个协议<UIImagePickerControllerDelegate,UINavigationControllerDelegate>。

View Code

代码如下复制代码

#pragma mark 从用户相册获取活动图片

- (void)pickImageFromAlbum

{

    imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.delegate =self;

    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    imagePicker.allowsEditing =YES;

    

    [self presentModalViewController:imagePicker animated:YES];

}

我们来看看上面的从相册获取图片,我们首先要实例化UIImagePickerController对象,然后设置imagePicker对象为当前对象,设置imagePicker的图片来源为UIImagePickerControllerSourceTypePhotoLibrary,表明当前图片的来源为相册,除此之外还可以设置用户对图片是否可编辑。

View Code

代码如下复制代码

#pragma mark 从摄像头获取活动图片

- (void)pickImageFromCamera

{

    imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.delegate =self;

    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    imagePicker.allowsEditing =YES;

    

    [self presentModalViewController:imagePicker animated:YES];

}

//打开相机

- (IBAction)touch_photo:(id)sender {

    // for iphone

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

   if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;

        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];

        

    }

    pickerImage.delegate =self;

    pickerImage.allowsEditing =YES;//自定义照片样式

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

}

以上是从摄像头获取图片,和从相册获取图片只是图片来源的设置不一样,摄像头图片的来源为UIImagePickerControllerSourceTypeCamera。

    在和用户交互之后,用户选择好图片后,会回调选择结束的方法。

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

{

    //初始化imageNew为从相机中获得的--

    UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //设置image的尺寸

    CGSize imagesize = imageNew.size;

    imagesize.height =626;

    imagesize.width =413;

    //对图片大小进行压缩--

    imageNew = [self imageWithImage:imageNew scaledToSize:imagesize];

    NSData *imageData = UIImageJPEGRepresentation(imageNew,0.00001);

   if(m_selectImage==nil)

    {

        m_selectImage = [UIImage imageWithData:imageData];

        NSLog(@"m_selectImage:%@",m_selectImage);

        [self.TakePhotoBtn setImage:m_selectImage forState:UIControlStateNormal];

        [picker dismissModalViewControllerAnimated:YES];

       return ;

    }

    [picker release];

}

//对图片尺寸进行压缩--

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

    // Create a graphics image context

    UIGraphicsBeginImageContext(newSize);

    

    // Tell the old image to draw in this new context, with the desired

    // new size

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    

    // Get the new image from the context

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    

    // End the context

    UIGraphicsEndImageContext();

    

    // Return the new image.

   return newImage;

}

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:python判断指定的端口是否被占用

下一篇:Java检测字符串中是否包含汉字