//圖片水印效果
-(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;
}