真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

iOS中常見的視圖和圖片處理示例詳解

前言

創(chuàng)新互聯(lián)建站是一家專注于做網(wǎng)站、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),梨林網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:梨林等地區(qū)。梨林做網(wǎng)站價(jià)格咨詢:028-86922220

眾所周知在開發(fā)中不可避免的會(huì)遇到一些圖片和視圖的處理,我這里總結(jié)的這些只是我遇到的一些,以供下次使用查看。下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

圖片的旋轉(zhuǎn)

是UIImage的擴(kuò)展類,直接使用UIImage的對(duì)象調(diào)用即可

UIImage

#import 
#import 
 
@implementation UIImage (ImageRotate)
-(UIImage *)imageRotateIndegree:(float)degree{
 //1.image-》context
 size_t width = (size_t)(self.size.width *self.scale);
 size_t height = (size_t)(self.size.height*self.scale);
 
 size_t bytesPerRow = width * 4;//表明每行圖片數(shù)據(jù)字節(jié)
 CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;//alpha
 //配置上下文參數(shù)
 CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo);
 if (!bmContext) {
 return nil;
 }
 CGContextDrawImage(bmContext, CGRectMake(0, 0, width, height), self.CGImage);
 //2旋轉(zhuǎn)
 UInt8 *data = (UInt8*)CGBitmapContextGetData(bmContext);
 vImage_Buffer src = {data,height,width,bytesPerRow};
 vImage_Buffer dest = {data,height,width,bytesPerRow};
 Pixel_8888 bgColor = {0,0,0,0};
 vImageRotate_ARGB8888(&src, &dest, NULL, degree, bgColor, kvImageBackgroundColorFill);
 //3context-》UIImage
 CGImageRef rotateImageref = CGBitmapContextCreateImage(bmContext);
 UIImage *rotateImage = [UIImage imageWithCGImage:rotateImageref scale:self.scale orientation:self.imageOrientation];
 return rotateImage;
}
@end

圖片的裁剪

依然是UIImage的擴(kuò)展類,直接使用UIImage的對(duì)象調(diào)用即可

UIImage

@implementation UIImage (ImageCut)
 
-(UIImage *)ImageCutSize:(CGRect)rect{
 CGImageRef subImageref = CGImageCreateWithImageInRect(self.CGImage, rect);
 CGRect smallRef = CGRectMake(0, 0, CGImageGetWidth(subImageref), CGImageGetHeight(subImageref));
 
 UIGraphicsBeginImageContext(smallRef.size);
 
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextDrawImage(context, smallRef, subImageref);
 UIImage *image = [UIImage imageWithCGImage:subImageref];
 
 UIGraphicsEndImageContext();
 return image;
}
@end

獲取截屏

截屏是UIView的擴(kuò)展類

UIView

@implementation UIView (imageScreenShot)
- (UIImage *)imageScreenShot
{
 UIGraphicsBeginImageContext(self.frame.size);
 [self.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imageNew;
}
@end

使用方法

UIView

- (void)imageScreen{
 UIImage *imageNew = [self.view imageScreenShot];
 UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil); //直接保存在相冊(cè)里,要獲取相冊(cè)權(quán)限
} 

圖片比例處理

依然是UIImage的擴(kuò)展類

UIImage

@implementation UIImage (imageScaleSize)
 
- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize{
 UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
 [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
  return scaledImage;
}
@end

view添加圓角

這里是UIView的擴(kuò)展類,適用于所有的View,可以設(shè)置添加的位置

UIView

@implementation UIView (LSCore)
 
/**
 設(shè)置部分圓角 絕對(duì)布局
 
 @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft|UIRectCornerTopRight
 @param radii 需要設(shè)置的圓角大小 CGSizeMake(5.0, 5.0)
 */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:rounded.CGPath];
 self.layer.mask = shape;
}
 
 
/**
 設(shè)置部分圓角 相對(duì)布局
 
 @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft|UIRectCornerTopRight
 
 @param radii 需要設(shè)置的圓角大小 CGSizeMake(5.0, 5.0)
 @param rect 需要設(shè)置的圓角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii viewRect:(CGRect)rect{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:rounded.CGPath];
 self.layer.mask = shape;
}
@end

使用方法以UIImageView為例

UIImage

[image addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(20.0, 20.0)];

將顏色轉(zhuǎn)為圖片

UIImage

-(UIImage *)ImageForColor:(UIColor *)color{
 CGRect rect = CGRectMake(0.0f, 0.0f, 10, 10);
 UIGraphicsBeginImageContext(rect.size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 
 CGContextSetFillColorWithColor(context, [color CGColor]);
 CGContextFillRect(context, rect);
 
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return image;
}

圖片添加系統(tǒng)濾鏡

UIImage

-(UIImage *)blurryImage:(UIImage *)image
   withBlurLevel:(CGFloat)blur {
 CIContext *context = [CIContext contextWithOptions:nil];
 CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
 CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
         keysAndValues:kCIInputImageKey, inputImage,
      @"inputRadius", @(blur),
      nil];
 
 CIImage *outputImage = filter.outputImage;
 CGImageRef outImage = [context createCGImage:outputImage
          fromRect:[outputImage extent]];
 
 return [UIImage imageWithCGImage:outImage];
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。


當(dāng)前標(biāo)題:iOS中常見的視圖和圖片處理示例詳解
文章鏈接:http://weahome.cn/article/ihcgjd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部