//图片水印效果
-(UIImage*)OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size); //size 为CGSize类型,即你所需要的图片尺寸
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
NSString
*text = [NSString stringWithFormat:@"%@\n%@\n%@",
self.DcAddDict[@"time"], self.DcAddDict[@"address"],
self.DcAddDict[@"name"]];
//绘制图片
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
//添加设定字体的键值对
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//添加前景色(字体色)
attributes[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
CGSize
textSize = [text boundingRectWithSize:CGSizeMake(size.width - 20,
size.height) options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes context:nil].size;
[text
drawInRect:CGRectMake(20 + 1, size.height - textSize.height - 5 +1,
textSize.width,textSize.height) withAttributes:attributes];
attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
[text drawInRect:CGRectMake(20, size.height - textSize.height - 5, textSize.width,textSize.height) withAttributes:attributes];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSLog(@"111111111111111111%@",scaledImage);
return scaledImage; //返回的就是已经改变的图片
}
//下载图片
#pragma mark ----------下载二维码图片
方法一:
- (void)btnClick{
if(imag.image){
__block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib writeImageToSavedPhotosAlbum:imag.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"assetURL = %@, error = %@", assetURL, error);
lib = nil;
}];
}
}
使用了ALAssetsLibrary类的writeImageToSavedPhotosAlbum:metadata:completionBlock:方法实现。其中第一个参数是一个CGImageRef的对象,表示要传入的图片。第二个参数是图片的一些属性,这里没有设置所以传入nil。最后一个completionBlock是保存完成后的回调,在这个回调中可以取到保存后的图片路径以及保存失败时的错误信息。
注意:使用该类时需要导入AssetsLibrary.framework。而且该类需要在iOS4.0以上可以使用,但是在iOS9.0之后就被标记为过时方法
方法二:
-(void)loadImageFinished:(UIImage*)image{
UIImageWriteToSavedPhotosAlbum(image, self,@selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
-(void)image:(UIImage*)imagedidFinishSavingWithError:(NSError*)errorcontextInfo:(void*)contextInfo{
NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}
//生成带logo的二维码
KMQRCode.h文件
@interface KMQRCode : NSObject
//生成二维码图片
+ (CIImage *)createQRCodeImage:(NSString *)source;
//使用核心绘图框架CG(Core Graphics)对象操作,进一步针对大小生成二维码图片imgAdaptiveQRCode(图片大小适合,清晰,效果好)
+ (UIImage *)resizeQRCodeImage:(CIImage *)image withSize:(CGFloat)size;
//二维码颜色
+ (UIImage *)specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
//使用核心绘图框架CG(Core Grahpics)对象操作,合并二维码�图片和用于中间显示的图标图片
+ (UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize;
+ (UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale;
@end
KMQRCode.m文件
#pragma mark - Private Methods
void ProviderReleaseData (void *info, const void *data, size_t size){
free((void*)data);
}
#pragma mark - Public Methods
+ (CIImage *)createQRCodeImage:(NSString *)source {
NSData *data = [source dataUsingEncoding:NSUTF8StringEncoding];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"Q" forKey:@"inputCorrectionLevel"]; //设置纠错等级越高;即识别越容易,值可设置为L(Low) | M(Medium) | Q | H(High)
return filter.outputImage;
}
+ (UIImage *)resizeQRCodeImage:(CIImage *)image withSize:(CGFloat)size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
CGContextRef contextRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(contextRef, kCGInterpolationNone);
CGContextScaleCTM(contextRef, scale, scale);
CGContextDrawImage(contextRef, extent, imageRef);
CGImageRef imageRefResized = CGBitmapContextCreateImage(contextRef);
//Release
CGContextRelease(contextRef);
CGImageRelease(imageRef);
return [UIImage imageWithCGImage:imageRefResized];
}
+ (UIImage *)specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
const int imageWidth = image.size.width;
const int imageHeight = image.size.height;
size_t bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
//Create context
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef
contextRef = CGBitmapContextCreate(rgbImageBuf, imageWidth,
imageHeight, 8, bytesPerRow, colorSpaceRef, kCGBitmapByteOrder32Little |
kCGImageAlphaNoneSkipLast);
CGContextDrawImage(contextRef, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
//Traverse pixe
int pixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
for (int i = 0; i < pixelNum; i++, pCurPtr++){
if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){
//Change color
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[3] = red; //0~255
ptr[2] = green;
ptr[1] = blue;
}else{
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[0] = 0;
}
}
//Convert to image
CGDataProviderRef
dataProviderRef = CGDataProviderCreateWithData(NULL, rgbImageBuf,
bytesPerRow * imageHeight, ProviderReleaseData);
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpaceRef,
kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProviderRef,
NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProviderRef);
UIImage* img = [UIImage imageWithCGImage:imageRef];
//Release
CGImageRelease(imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpaceRef);
return img;
}
+(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize {
UIGraphicsBeginImageContext(image.size);
//通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并
CGFloat widthOfImage = image.size.width;
CGFloat heightOfImage = image.size.height;
CGFloat widthOfIcon = iconSize.width;
CGFloat heightOfIcon = iconSize.height;
[image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
[icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
widthOfIcon, heightOfIcon)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
+(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale {
UIGraphicsBeginImageContext(image.size);
//通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并
CGFloat widthOfImage = image.size.width;
CGFloat heightOfImage = image.size.height;
CGFloat widthOfIcon = widthOfImage/scale;
CGFloat heightOfIcon = heightOfImage/scale;
[image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
[icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
widthOfIcon, heightOfIcon)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
网友评论