目录
1.1 动画效果
1.2 一像素线切割图片
1.3 移动
1.4 星级
1.5 将图片保存到相册
1.6 设置图片圆角
1.1 动画效果
NSArray *imageArray = [NSArray arrayWithObjects: [UIImage imageNamed:@"D1@3x.png"], [UIImage imageNamed:@"D2@3x.png"], nil];
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(15 , 15, 50, 50)];
imageV.animationImages = imageArray;
//重复次数
imageV.animationRepeatCount = 0;
//
imageV.animationDuration = 0.15*imageArray.count;
[imageV startAnimating];
[self addSubview:imageV];
1.2 一像素线切割图片
leftImage = [leftImage stretchableImageWithLeftCapWidth:30 topCapHeight:35];
1.3 移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
if (touch.view==self.bgview) {
CGPoint point=[touch locationInView:self.view];
self.bgview.center=CGPointMake(self.bgview.frame.size.width/2.0f, point.y-self.bgview.frame.size.height/2.0f);
}
}
}
1.4 星级
![Uploading uiimageView1_404203.png . . .]
.h
#import < UIKit/UIKit.h >
@interface StarView : UIView
- (void)setRating:(CGFloat)rating;
@end
.m
#import "StarView.h"
@interface StarView ()
{
//背景图
UIImageView *_backgroundView;
//前景图
UIImageView *_foreView;
}
@end
@implementation StarView
- (void)createContent
{
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
65, 23)];
_backgroundView.contentMode=UIViewContentModeLeft;
_backgroundView.image = [UIImage imageNamed:@"StarsBackground"];
_foreView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 65,
23)];
_foreView.contentMode=UIViewContentModeLeft;
_foreView.image = [UIImage imageNamed:@"StarsForeground"];
_foreView.clipsToBounds = YES;
[self addSubview:_backgroundView];
[self addSubview:_foreView];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createContent];
}
return self;
}
- (void)setRating:(CGFloat)rating
{
CGRect frame = _backgroundView.frame;
frame.size.width = frame.size.width*(rating/5.0f);
_foreView.frame = frame;
}
使用
StarView *stat = [[StarView alloc]initWithFrame:CGRectMake(100, 200,
65, 23)];
[self.view addSubview:stat];
[stat setRating:3];
1.5 将图片保存到相册
- (void)buttonClicked:(UIButton *)button{
UIImage *image = [UIImage imageNamed:@"1.png"];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"失败");
} else {
NSLog(@"成功");
}
}
1.6 设置图片圆角
/** 这个方法效率很高,比layer快很多 */
- (UIImage *)cutCircleImage {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGContextRef ctr = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctr, rect);
CGContextClip(ctr);
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}