APP很多时候需要加上角标,并且同一个icon供很多尺寸去使用,这个时候如果需要美工一个个切和合成十分繁琐,我们可以直接用OC来实现:
#import "Masonry.h"
#import "QQChating.h"
#import "ContainImageView.h"
#import "ShowImageViewController.h"
@interface ShowImageViewController () <UIAlertViewDelegate>
{
BOOL clicked;
}
@property (nonatomic, strong) UITextField* textField;
@property (nonatomic, strong) ContainImageView* imageView;
@property (nonatomic, copy) UIImage* tmpImage;
@end
@implementation ShowImageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
clicked = NO;
UIButton* button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setBackgroundColor:[UIColor redColor]];
[button0 setTitle:@"0击" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button0 setTag:1000];
button0.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button0];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setBackgroundColor:[UIColor blueColor]];
[button1 setTitle:@"1击" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTag:1001];
button1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundColor:[UIColor greenColor]];
[button2 setTitle:@"2击" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setTag:1002];
button2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setBackgroundColor:[UIColor grayColor]];
[button3 setTitle:@"3击" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setTag:1003];
button3.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:button3];
UIView* superview = self.view;
CGFloat padding = 10.0;
[button0 mas_makeConstraints:^(MASConstraintMaker* make){
make.centerY.mas_equalTo(superview);
make.left.equalTo(superview).with.offset(padding);
make.right.equalTo(button1.mas_left).with.offset(-padding);
make.height.mas_equalTo(@50);
make.width.equalTo(@[button1, button2, button3]);
}];
[button1 mas_makeConstraints:^(MASConstraintMaker* make){
make.centerY.mas_equalTo(superview);
make.right.mas_equalTo(button2.mas_left).with.offset(-padding);
make.height.mas_equalTo(button0);
make.width.equalTo(@[button0, button2, button3]);
}];
[button2 mas_makeConstraints:^(MASConstraintMaker* make){
make.centerY.mas_equalTo(superview);
make.height.mas_equalTo(button0);
make.width.equalTo(@[button0, button1, button3]);
}];
[button3 mas_makeConstraints:^(MASConstraintMaker* make){
make.centerY.mas_equalTo(superview);
make.height.mas_equalTo(button0);
make.left.equalTo(button2.mas_right).with.offset(padding);
make.right.equalTo(superview).with.offset(-padding);
make.height.mas_equalTo(button0);
make.width.equalTo(@[button0, button1, button2]);
}];
self.imageView = [ContainImageView new];
self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.imageView setImage:[UIImage imageNamed:@"icon.png"]];
[superview addSubview:self.imageView];
[self.imageView mas_makeConstraints:^(MASConstraintMaker* make){
make.width.mas_equalTo(@100);
make.height.mas_equalTo(@100);
make.centerX.mas_equalTo(superview);
make.top.equalTo(superview).with.offset(10*padding);
}];
self.textField = [UITextField new];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleLine;
self.textField.placeholder = @"input the size";
self.textField.keyboardType = UIKeyboardTypeNumberPad;
self.textField.textAlignment = NSTextAlignmentCenter;
[superview addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker* make){
make.width.mas_equalTo(@150);
make.height.mas_equalTo(@35);
make.centerX.mas_equalTo(superview);
make.bottom.equalTo(superview).with.offset(-160);
}];
self.tmpImage = [UIImage new];
}
//输入后,上下滑动屏幕可以自动改变
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UISwipeGestureRecognizer* updown = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(updown)];
[updown setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:updown];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)updown
{
[self.textField resignFirstResponder];
if (clicked == NO)
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil
message:@"先添加角标!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
return;
}
if ([self.textField.text isEqualToString:@""] || [self.textField.text length] == 0 || self.textField.text == NULL)
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil
message:@"大小不能空!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
return;
}
//用一个self.tmpImage来充当 [UIImage imageNamed:@"icon.png"]
//这种“常量”的中间temp值,这样既可以模糊化又可以清晰化
UIImage* image = [self.tmpImage copy];
image = [self imageWithImage:image scaledToSize:CGSizeMake([self.textField.text floatValue],
[self.textField.text floatValue])];
[self.imageView setImage:image];
}
//改变尺寸的方法:
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//这一步很关键,合成图像之后是不会改变当前的UIImageView的结构的
//所以需要在这里把之前添加的subviews删除掉
for (id subviews in self.imageView.subviews)
{
[((UIView*)subviews) removeFromSuperview];
}
return newImage;
}
//截屏方法1:
- (UIImage *)snapshot
{
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, YES, 0);
[self.imageView drawViewHierarchyInRect:self.imageView.bounds afterScreenUpdates:YES];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//实现方法2:
//- (UIImage* )snapshot
//{
// UIImage* snapshot = nil;
//
// UIGraphicsBeginImageContext(self.imageView.frame.size);
// {
// [self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
// snapshot = UIGraphicsGetImageFromCurrentImageContext();
// }
//
// UIGraphicsEndImageContext();
//
// return snapshot;
//}
#pragma mark - ButtonClicked:
- (void)changeImageViewSize:(id)sender
{
NSLog(@"11");
}
- (void)buttonClicked:(UIButton*)button
{
switch (button.tag)
{
case 1000:
{
[self.imageView addIconWithImage:[UIImage imageNamed:@"512-橙色右上.png"] andTag:RIGHT_TOP];
break;
}
case 1001:
{
[self.imageView addIconWithImage:[UIImage imageNamed:@"512-橙色右下.png"] andTag:RIGHT_DOWN];
break;
}
case 1002:
{
[self.imageView addIconWithImage:[UIImage imageNamed:@"512-橙色左上.png"] andTag:LEFT_TOP];
break;
}
case 1003:
{
[self.imageView addIconWithImage:[UIImage imageNamed:@"512-橙色左下.png"] andTag:LEFT_DOWN];
break;
}
default:
break;
}
//拿一个tmpImage来存添加角标后的原始值,但是不是万能的,因为如果你同时添加多个角标,那就乱定了!
self.tmpImage = [self snapshot];
clicked = YES;
}
#pragma mark - Orientation Change:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
BOOL isLandscape = NO;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
isLandscape = ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft));
if(isLandscape)
{
//横屏
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.imageView mas_updateConstraints:^(MASConstraintMaker* make){
make.top.equalTo(self.view).with.offset(10);
}];
[self.textField mas_updateConstraints:^(MASConstraintMaker* make){
make.bottom.equalTo(self.view).with.offset(-70);
}];
}
else
{
//纵屏
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.imageView mas_updateConstraints:^(MASConstraintMaker* make){
make.top.equalTo(self.view).with.offset(100);
}];
[self.textField mas_updateConstraints:^(MASConstraintMaker* make){
make.bottom.equalTo(self.view).with.offset(-160);
}];
}
}
@end
还有一种方法,以后pop上来!