1.图片的圆角效果
<code>
-(UIImage) circleImage:(UIImage) image withParam:(CGFloat) inset {
UIGraphicsBeginImageContextWithOptions(image.size, 0, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[image drawInRect:rect];
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
</code>
2.图片的拉伸
<code>
// UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
// UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
CGFloat top = 25; // 保留顶端高度
CGFloat bottom = 25 ; // 保留底端高度
CGFloat left = 10; // 保留左端宽度
CGFloat right = 10; // 保留右端宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 指定为拉伸模式,伸缩后重新赋值,一般情况下我们只需要拉伸中间1*1区域就行了
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
</code>
3.从一张大图中得到小图片
<code>
CGRect rect;
UIImage *smallImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, rect)];
</code>
4.图片的大小
<code>
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);
//图片大小(得到的是KB)
CGFloat length = [data length]/1000;
//通常当图片太大时,我们可以先把图片画到一个小的画布上,再得到画布上的图片。可改变图片的大小
//把图片写打path路径下
[data writeToFile:path atomically:YES];
// 若要存储到图片库里面
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
</code>
5.改变图片的透明度
<code>
UIGraphicsBeginImageContextWithOptions(image.size, 0, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAlpha(context, alpha);
[image drawInRect:(CGRect){{0,0},image.size}];
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
</code>
网友评论