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指定的矩形區(qū)域來填充圖片
// UIImageResizingModeTile:平鋪模式,通過重復顯示UIEdgeInsets指定的矩形區(qū)域來填充圖片
CGFloat top = 25; // 保留頂端高度
CGFloat bottom = 25 ; // 保留底端高度
CGFloat left = 10; // 保留左端寬度
CGFloat right = 10; // 保留右端寬度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 指定為拉伸模式,伸縮后重新賦值,一般情況下我們只需要拉伸中間1*1區(qū)域就行了
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;
//通常當圖片太大時,我們可以先把圖片畫到一個小的畫布上,再得到畫布上的圖片??筛淖儓D片的大小
//把圖片寫打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>