无奈有时记性不好,一些经常用的随记。
九宫格
for (int x = 0; x < 10; x++) {
//创建按钮
int i = x / 3; //---- 0 1 2 行
int j = x % 3; //-----0 1 2 列
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(j * (BUTTONWIDTH + columnWidth) + columnWidth, i * (BUTTONHEIGHT + rowHeight) + rowHeight, BUTTONWIDTH, BUTTONHEIGHT);
btn.tag = x + 1;
[btn setTitle:[NSString stringWithFormat:@"%d",btn.tag] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
裁剪图片
-(UIImage *)getImageFromImage:(UIImage*) superImage
{
CGSize subImageSize = CGSizeMake(320, 200);
//定义裁剪的区域相对于原图片的位置
CGRect subImageRect = CGRectMake(0, 60, superImage.size.width, superImage.size.height);
CGImageRef imageRef = superImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
UIGraphicsBeginImageContext(subImageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, subImageRect, subImageRef);
UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
//返回裁剪的部分图像
return subImage;
}
//渲染缓存
self.layer.shouldRasterize=YES;
self.layer.rasterizationScale= [UIScreenmainScreen].scale;
//NSTextAttachment 调整centerY
NSTextAttachment * ment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
ment.image = [];
ment.bounds = CGRectMake(0, self.Label.font.descender/2.0, self.Label.font.ascender, self.Label.font.ascender);