iOS

iOS模仿QQ设置用户头像

2016-11-08  本文已影响107人  星星编程
头像剪切 头像剪切

1、待用户对图片移动和缩放后截屏操作得到用户想要的效果。

-(UIImage *)screenCapture{

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO , [[UIScreen mainScreen] scale]);

CGContextRef context = UIGraphicsGetCurrentContext();

[self.view.layer renderInContext:context];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

2、计算用户圈住的部分在整个截屏的位置

-(CGRect)clipRect:(UIView *)view{

CGRect rect = view.frame;

rect.origin.x*=[UIScreen mainScreen].scale;

rect.origin.y*=[UIScreen mainScreen].scale;

rect.size.width*=[UIScreen mainScreen].scale;

rect.size.height*=[UIScreen mainScreen].scale;

return rect;

}

3、把圈内的部分从截屏的图片内剪切出来

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {

CGImageRef sourceImageRef = [image CGImage];

CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);

UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

return newImage;

}

上一篇下一篇

猜你喜欢

热点阅读