UIView 转 UIImage

这个方法很实用,特别是在做水印相机得时候。。。

- (UIImage*) imageWithUIView:(UIView*) view{
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(view.bounds.size);  
    CGContextRef currnetContext = UIGraphicsGetCurrentContext();
    //[view.layer drawInContext:currnetContext];
    [view.layer renderInContext:currnetContext];
    // 从当前context中创建一个改变大小后的图片  
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();  
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
    return image;
}

这个方法转换成的UIImage不是高清的,想要得到高清的图片用下面的方法

+ (UIImage*)convertViewToImage:(UIView*)view

{

    CGSize size = view.bounds.size;

    // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了

    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

    return image;

}

 

 

转载于:https://www.cnblogs.com/joesen/p/3807392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一个简单的大盘游戏的object-c实现示例: 首先我们需要一个盘的图片资源,可以用UIImageView来展示。 ```objective-c // 初始化盘图片视图 UIImageView *rotateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; rotateImageView.image = [UIImage imageNamed:@"rotateImage.png"]; rotateImageView.center = self.view.center; [self.view addSubview:rotateImageView]; ``` 接下来,我们需要给盘添加一个旋动画,让它可以动起来。 ```objective-c // 定义旋动画 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0]; rotationAnimation.duration = 2.0; rotationAnimation.repeatCount = HUGE_VALF; // 添加旋动画 [rotateImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; ``` 现在我们需要给大盘添加一些奖励,假设我们有6个奖励,可以用一个数组来存储。 ```objective-c // 定义奖项数组 NSArray *prizeArray = @[@"奖品1", @"奖品2", @"奖品3", @"奖品4", @"奖品5", @"奖品6"]; ``` 接下来,我们需要处理用户点击开始按钮后的逻辑,让盘旋并随机停止到某个奖项上。 ```objective-c // 处理开始按钮点击事件 - (IBAction)startButtonClick:(id)sender { // 随机生成旋角度 CGFloat angle = M_PI * 2.0 / prizeArray.count * arc4random_uniform(prizeArray.count); // 计算旋动画结束位置 CGAffineTransform endTransform = CGAffineTransformMakeRotation(angle); // 定义旋动画 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat:angle + M_PI * 2.0]; rotationAnimation.duration = 3.0; rotationAnimation.autoreverses = NO; rotationAnimation.cumulative = YES; rotationAnimation.delegate = self; // 添加旋动画 [rotateImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; // 更新盘视图的旋位置 [UIView animateWithDuration:3.0 animations:^{ rotateImageView.transform = endTransform; }]; } // 旋动画结束后回调方法 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { // 计算旋后指针所在的奖项 NSInteger index = (NSInteger)(rotateImageView.transform.a / (M_PI * 2.0 / prizeArray.count) + 0.5) % prizeArray.count; // 弹出奖项提示框 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"中奖啦!" message:prizeArray[index] preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; } ``` 这样就实现了一个简单的大盘游戏。当用户点击开始按钮后,盘会旋一段时间后随机停止到某个奖项上,并弹出奖项提示框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值