iOS常用的一些小方法

一、button扩大点击区域

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event

{

CGRect bounds = self.bounds;

    //若原热区小于44x44,则放大热区,否则保持原大小不变

CGFloat widthDelta = MAX(44.0 - bounds.size.width,0);

CGFloat heightDelta = MAX(44.0 - bounds.size.height,0);

bounds = CGRectInset(bounds,-0.5 * widthDelta,-0.5 * heightDelta);

return CGRectContainsPoint(bounds,point);

}

二、颜色转图片

+ (UIImage*) getImageWithColor:(UIColor*)color andHeight:(CGFloat)height

{

    CGRectr=CGRectMake(0.0f,0.0f,1.0f, height);

    UIGraphicsBeginImageContext(r.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, r);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnimg;

}

三、获取视频第一帧图片

+ (UIImage*)getVideoPreViewImage:(NSURL*)path

{

    AVURLAsset*asset = [[AVURLAssetalloc]initWithURL:pathoptions:nil];

    AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    assetGen.appliesPreferredTrackTransform = YES;

    CMTime time = CMTimeMakeWithSeconds(0.0, 600);

    NSError*error =nil;

    CMTimeactualTime;

    CGImageRefimage = [assetGencopyCGImageAtTime:timeactualTime:&actualTimeerror:&error];

    UIImage*videoImage = [[UIImagealloc]initWithCGImage:image];

    CGImageRelease(image);

    returnvideoImage;

}

四、生成二维码图片

+ (UIImage*)createQRImageWithString:(NSString*)string size:(CGSize)size

{

NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];

CIFilter*qrFilter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];

[qrFiltersetValue:stringDataforKey:@"inputMessage"];

[qrFiltersetValue:@"M" forKey:@"inputCorrectionLevel"];

CIImage*qrImage = qrFilter.outputImage;

//放大并绘制二维码 (上面生成的二维码很小,需要放大)

CGImageRefcgImage = [[CIContextcontextWithOptions:nil]createCGImage:qrImagefromRect:qrImage.extent];

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetInterpolationQuality(context, kCGInterpolationNone);

CGContextScaleCTM(context,1.0, -1.0);

CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);

UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRelease(cgImage);

 returncodeImage;

}

你可能感兴趣的:(iOS常用的一些小方法)