点击头像放大

#pragma mark - 放大头像
- (IBAction)showBigPortrait:(id)sender {
    //创建灰色透明背景,使其背后内容不可操作
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [bgView setBackgroundColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.7]];
    [self.view.window addSubview:bgView];
    
    //创建边框视图
    UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 286, 286)];
    //将图层的边框设置为圆角
    borderView.layer.cornerRadius = 8;
    borderView.layer.masksToBounds = YES;
    //给图层添加一个有色边框
    borderView.layer.borderWidth = 8;
    borderView.layer.borderColor = [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.7] CGColor];
    [borderView setCenter:bgView.center];
    [bgView addSubview:borderView];
    
    //创建触摸事件
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeBigImage)];
    [bgView addGestureRecognizer:recognizer];
    
    //创建显示图像视图
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 270, 270)];
    [imgView setImage:partraitImg.image];
    [borderView addSubview:imgView];
}
//移除大图
- (void)removeBigImage
{
    [bgView removeFromSuperview];
}

### 实现点击头像放大图片预览的方法 为了实现点击头像后将其放大的功能,可以利用 `el-dialog` 和 `img` 标签来创建一个简单的图像查看器。当用户点击上传的头像时,弹出对话框显示该图片的大图版本。 #### HTML 结构 ```html <template> <div> <!-- 头像展示区域 --> <el-image style="width: 100px; height: 100px" :src="imageUrl" @click="dialogVisible = true"> </el-image> <!-- 对话框用于展示大图 --> <el-dialog :visible.sync="dialogVisible" width="50%"> <img :src="imageUrl" alt="" style="max-width: 100%; max-height: 80vh;"> </el-dialog> <!-- 文件上传控件 --> <el-upload class="avatar-uploader" action="" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <i v-if="!imageUrl" class="el-icon-plus avatar-uploader-icon"></i> <img v-else :src="imageUrl" class="avatar"> </el-upload> </div> </template> ``` #### JavaScript 部分 ```javascript <script> export default { data() { return { imageUrl: '', dialogVisible: false, }; }, methods: { handleAvatarSuccess(res, file) { this.imageUrl = URL.createObjectURL(file.raw); }, beforeAvatarUpload(file) { const isJPG = file.type === 'image/jpeg'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG 格式!'); } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); } return isJPG && isLt2M; } } }; </script> ``` 通过上述代码片段,在用户单击已上传的小尺寸头像时会触发事件打开一个新的模态窗口[^1],其中展示了原始分辨率下的完整图像供浏览者查看[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值