有些時候經常需要對系統本有的類方法進行擴充,於是可能需要對類採用繼承或者分類的方式來實現需要。
最近寫的一個項目用到了一些對圖片進行處理的一些擴充方法。
涉及到的方法:
比較常用的類似於拉伸圖片,拼接圖片名稱或者以顏色生成圖片等。
拼接圖片名稱:
- + (UIImage *)imageMatchSizeWithName:(NSString *)imageName
- {
- if (__Device_Iphone_5__) //iphone5,5s
- {
- NSString *ext = [imageName pathExtension];
- imageName = [imageName stringByDeletingPathExtension];
- imageName = [imageName stringByAppendingString:@"-568h@2x"];
- imageName = [imageName stringByAppendingPathExtension:ext];
- }
- return [UIImage imageNamed:imageName];
- }
通過宏來給圖片名稱拼接一個4寸標識。
拉伸圖片到指定尺寸:
- + (UIImage *)compressImage:(UIImage *)imgSrc toSize:(CGSize)size
- {
- UIGraphicsBeginImageContext(size);
- CGRect rect = {{0,0}, size};
- [imgSrc drawInRect:rect];
- UIImage *compressedImg = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return compressedImg;
- }
通過上下文來繪製實現將圖片拉伸到指定的尺寸。
指定位置獲取像素點平鋪拉伸圖片
- + (UIImage *)strechImageWithName:(NSString *)imageName
- {
- UIImage *image = [UIImage imageNamed:imageName];
- return [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
- }
- + (UIImage *)strechImageWithName:(NSString *)imageName posX:(CGFloat)x posY:(CGFloat)y
- {
- UIImage *image = [UIImage imageNamed:imageName];
- return [image stretchableImageWithLeftCapWidth:image.size.width*x topCapHeight:image.size.height*y];
- }
這裏涉及到一個端帽,不過如果不清楚只要知道是根據指定的像素位置平鋪來實現拉伸即可。
截屏:
- + (UIImage *)screenshot
- {
- CGSize imageSize = [[UIScreen mainScreen] bounds].size;
- if (NULL != UIGraphicsBeginImageContextWithOptions) {
- UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
- } else {
- UIGraphicsBeginImageContext(imageSize);
- }
- CGContextRef context = UIGraphicsGetCurrentContext();
- for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
- if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, [window center].x, [window center].y);
- CGContextConcatCTM(context, [window transform]);
- CGContextTranslateCTM(context,
- -[window bounds].size.width * [[window layer] anchorPoint].x,
- -[window bounds].size.height * [[window layer] anchorPoint].y);
- [[window layer] renderInContext:context];
- CGContextRestoreGState(context);
- }
- }
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
增加水印:
- + (UIImage *)addImage:(UIImage *)image addMsakImage:(UIImage *)maskImage maskFrame:(CGRect)pos
- {
- UIGraphicsBeginImageContext(image.size);
- [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
- //水印圖片的位置
- [maskImage drawInRect:pos];
- UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return resultingImage;
- }
指定顏色生成圖片:
- + (UIImage *)imageWithColor:(UIColor *)color
- {
- CGRect rect = CGRectMake(0, 0, 1, 1);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
以上三個方法其實均有些類似,都是通過圖片上下文來進行操作。
相對於上述方法,進行模糊稍微麻煩一些,也可以使用CoreImage中的高斯模糊濾鏡,CoreImage的簡單使用可以參照之前的博客:
- + (UIImage *)blurImage:(UIImage *)src amount:(CGFloat)amount
- {
- if (amount < 0.0 || amount > 1.0) {
- amount = 0.5;
- }
- int boxSize = (int)(amount * 40);
- boxSize = boxSize - (boxSize % 2) + 1;
- CGImageRef img = src.CGImage;
- vImage_Buffer inBuffer, outBuffer;
- vImage_Error error;
- voidvoid *pixelBuffer;
- CGDataProviderRef inProvider = CGImageGetDataProvider(img);
- CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
- inBuffer.width = CGImageGetWidth(img);
- inBuffer.height = CGImageGetHeight(img);
- inBuffer.rowBytes = CGImageGetBytesPerRow(img);
- inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
- pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
- outBuffer.data = pixelBuffer;
- outBuffer.width = CGImageGetWidth(img);
- outBuffer.height = CGImageGetHeight(img);
- outBuffer.rowBytes = CGImageGetBytesPerRow(img);
- error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- if (!error) {
- error = vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- if (!error) {
- error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- }
- }
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
- outBuffer.width,
- outBuffer.height,
- 8,
- outBuffer.rowBytes,
- colorSpace,
- (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
- CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
- UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
- CGContextRelease(ctx);
- CGColorSpaceRelease(colorSpace);
- free(pixelBuffer);
- CFRelease(inBitmapData);
- CGColorSpaceRelease(colorSpace);
- CGImageRelease(imageRef);
- return returnImage;
- }
實現即使不是太清楚,也可以直接拿來用就行。
資源位置
GitHub:UIImage-HR
CSDN:iOS圖片分類
以上就是本篇博客全部內容,歡迎指正和交流。轉載註明出處~http://blog.csdn.net/cocoarannie/article/details/18566043