下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过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,newSize.wIDth,newSize.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetimageFromCurrentimageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的IOS 图片上传处理 图片压缩 图片处理全部内容,希望文章能够帮你解决IOS 图片上传处理 图片压缩 图片处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)