美文网首页
选取照片之后的回调方法

选取照片之后的回调方法

作者: 施主小欣 | 来源:发表于2016-09-01 10:52 被阅读349次

    刚开始工作的时候因为安卓不能识别高清分辨图,iOS上传后安卓获取会崩溃,所以用的
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo这个老方法,这个方法在3.0弃用了,但在工程中并不影响使用。今天突然心血来潮试着改写一下【获取相册后图片回调】。

    在获取相册后图片回调的方法用
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

     UIImage *ima = info[UIImagePickerControllerEditedImage];
    
    //用的是UIImageExt 这个工具类
    ima = [UIImageExt setAvatarForNewSize:ima];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    self.titleImageView.layer.cornerRadius = 20 * Width;
    
    //如果是button上的图片的话
    [self.headButton setImage:[image imageWithRenderingMode:1] forState:UIControlStateNormal];
    
    }
    

    UIImageExt 工具类【我在原有的这个封装好的工类里面添加一个类方法setAvatarForNewSize】

    + (UIImage *)setAvatarForNewSize:(UIImage *)image {
    CGSize origImageSize = image.size;
    
    // new image size needed, which is picked by the image picker
    CGFloat newWidth = kScreenWidth;
    CGFloat newHeight = (newWidth * origImageSize.height) / origImageSize.width;
    CGRect newRect = CGRectMake(0, 0, newWidth, newHeight);
    
    // make sure that zoom ratio stays original value
    float ratio = MAX(newRect.size.width / origImageSize.width, newRect.size.height / origImageSize.height);
    
    // create a lucency bitmap context via current screen scaling factor
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
    
    // make the new image be in the center in the draw zoom
    CGRect projectRect;
    projectRect.size.width = ratio * origImageSize.width;
    projectRect.size.height = ratio * origImageSize.height;
    projectRect.origin.x = (newRect.size.width - projectRect.size.width) * 0.5;
    projectRect.origin.y = (newRect.size.height - projectRect.size.height) * 0.5;
    
    // draw the new picture in the new rect
    [image drawInRect:projectRect];
    
    // get the image we need
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // clean up image context
    UIGraphicsEndImageContext();
    
    return smallImage;
    }
    

    这样在系统相册获取图片的清晰辨识就会搞了许多。

    实现原理,就是用图形上下文把原图按长宽比例绘制成原来长宽比屏幕大小的图片,然后再截取新图,可能以前是1000:2000的分辨率,给缩成100:200了,细节基本没变化

    相关文章

      网友评论

          本文标题:选取照片之后的回调方法

          本文链接:https://www.haomeiwen.com/subject/xtslkttx.html