日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIGraphicsBeginImageContext系列知识

發(fā)布時間:2025/7/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UIGraphicsBeginImageContext系列知识 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

UIGraphicsBeginImageContext
創(chuàng)建一個基于位圖的上下文(context),并將其設(shè)置為當(dāng)前上下文(context)。方法聲明如下:

?

參數(shù)size為新創(chuàng)建的位圖上下文的大小。它同時是由UIGraphicsGetImageFromCurrentImageContext函數(shù)返回的圖形大小。
該函數(shù)的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相當(dāng)與UIGraphicsBeginImageContextWithOptions的opaque參數(shù)為NO,scale因子為1.0。

UIGraphicsBeginImageContextWithOptions
函數(shù)原型為:

?

size——同UIGraphicsBeginImageContext
opaque—透明開關(guān),如果圖形完全不用透明,設(shè)置為YES以優(yōu)化位圖的存儲。
scale—–縮放因子

默認(rèn)創(chuàng)建一個透明的位圖上下

?

UILabel根據(jù)text自動調(diào)整大小

?

C代碼 ?

  • label.text = @"**********";
  • CGRect frame = label.frame;
  • frame.size.height = 10000; // 設(shè)置一個很大的高度
  • label.frame = frame;
  • [label sizeToFit];
  • frame.size.height = label.frame.size.height;
  • label.frame = frame;

?

?

直接撥打有分機(jī)號的電話

?

C代碼 ?

  • [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://01011112222,3333"]];

?

一些有關(guān)圖像處理的代碼片段

?

- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size; //圖片縮放裁剪

- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改變大小

+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并圖片

+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分圖片

+ (void)imageSavedToPhotosAlbum:(UIImage *)image

didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; //保存圖片到媒體庫

?

零)重新設(shè)置圖片的尺寸

- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size {

  CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);

  UIGraphicsBeginImageContext(rect.size);

  [img drawInRect:rect]; // scales image to rect

  UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return resImage;

}

-)根據(jù)給定得圖片,從其指定區(qū)域截取一張新得圖片

-(UIImage *)getImageFromImage{

  //大圖bigImage

  //定義myImageRect,截圖的區(qū)域

  CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);

  UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];

  CGImageRef imageRef = bigImage.CGImage;

  CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

  CGSize size;

  size.width = 57.0;

  size.height = 57.0;

  UIGraphicsBeginImageContext(size);

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextDrawImage(context, myImageRect, subImageRef);

  UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

  UIGraphicsEndImageContext();

  return smallImage;

}

二) 合并兩張圖片

- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {

  UIGraphicsBeginImageContext(image1.size);

  // ?Draw image1

  [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

  // Draw image2

  [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

  UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return resultingImage;

}

?

三) 捕捉屏幕截圖

CALayer實(shí)例使用Core Graphics的renderInContext方法可以將視圖繪制到圖像上下文中以便轉(zhuǎn)化為其他UIImage實(shí)例。前提先#import

?

+ (UIImage *) imageFromView: (UIView *)theView {

  // draw a view's contents into an image context

  UIGraphicsBeginImageContext(theView.frame.size);

  CGContextRef context = UIGraphicsGetCurrentContext();

  [theView.layer renderInContext:context];

  UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return theImage;

}

?

注:UIGraphicsBeginImageContext(CGSize size)創(chuàng)建一個基于位圖的上下文(context),并將其設(shè)置為當(dāng)前上下文。函數(shù)功能與UIGraphicsBeginImageContextWithOptions相同,相當(dāng)于該方法的opaque參數(shù)為NO,scale因子為1.0。而UIGraphicsEndImageContext()方法是移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文。

視圖添加倒影效果

?

const CGFloat kReflectPercent = -0.25f;

const CGFloat kReflectOpacity = 0.3f;

const CGFloat kReflectDistance = 10.0f;

+ (void)addSimpleReflectionToView:(UIView *)theView {

  CALayer *reflectionLayer = [CALayer layer];

  reflectionLayer.contents = [theView layer].contents;

   reflectionLayer.opacity = kReflectOpacity;

  reflectionLayer.frame = CGRectMake(0.0f,0.0f,theView.frame.size.width,theView.frame.size.height*kReflectPercent);

  //倒影層框架設(shè)置,其中高度是原視圖的百分比

  CATransform3D stransform = CATransform3DMakeScale(1.0f,-1.0f,1.0f);

  CATransform3D transform = CATransform3DTranslate(stransform,0.0f,-(kReflectDistance + theView.frame.size.height),0.0f);

   reflectionLayer.transform = transform;

  reflectionLayer.sublayerTransform = reflectionLayer.transform;

  [[theView layer] addSublayer:reflectionLayer];

}

?

另一:使用Core Graphics創(chuàng)建倒影

?

+ (CGImageRef) createGradientImage:(CGSize)size {

   CGFloat colors[] = {0.0,1.0,1.0,1.0};

   //在灰色設(shè)備色彩上建立一漸變

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

   CGContextRef context = CGBitmapContextCreate(nil,size.width,size.height,8,0,colorSpace,kCGImageAlphaNone);

   CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,colors,NULL,2);

   CGColorSpaceRelease(colorSpace);

   //繪制線性漸變

  CGPoint p1 = CGPointZero;

   CGPoint p2 = CGPointMake(0,size.height);

   CGContextDrawLinearGradient(context,gradient,p1,p2,kCGGradientDrawsAfterEndLocation);

   //Return the CGImage

   CGImageRef theCGImage = CGBitmapContextCreateImage(context);

  CFRelease(gradient);

   CGContextRelease(context);

   return theCGImage;

 }

?

//Create a shrunken frame for the reflection

?

+ (UIImage *) reflectionOfView:(UIView *)theView WithPercent:(CGFloat) percent {

   //Retain the width but shrink the height

   CGSize size = CGSizeMake(theView.frame.size.width, theView.frame.size.height * percent);

   //Shrink the View UIGraphicsBeginImageContext(size);

   CGContextRef context = UIGraphicsGetCurrentContext();

   [theView.layer renderInContext:context];

   UIImage *partialimg = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

   //build the mask

   CGImageRef mask = [ImageHelper createGradientImage:size];

   CGImageRef ref = CGImageCreateWithMask(partialimg.CGImage,mask);

  UIImage *theImage = [UIImage imageWithCGImage:ref];

   CGImageRelease(ref);

   CGImageRelease(mask);

   return theImage;

}

const CGFloat kReflectDistance = 10.0f;

+ (void) addReflectionToView: (UIView *)theView {

   theView.clipsToBounds = NO;

   UIImageView *reflection = [[UIImageView alloc] initWithImage:[ImageHelper reflectionOfView:theView withPercent:0.45f]];

   CGRect frame = reflection.frame;

  frame.origin = CGPointMake(0.0f, theView.frame.size.height + kReflectDistance);

   reflection.frame = frame;

   // add the reflection as a simple subview

   [theView addSubView:reflection];

   [reflection release];

}

關(guān)于圖片縮放的線程安全和非線程安全操作.

非線程安全的操作只能在主線程中進(jìn)行操作,對于大圖片的處理肯定會消耗大量的時間,如下面的方法

方法 1: 使用 UIKit

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)image scaledToSize INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)newSize;

{

  // Create a graphics image context

  UIGraphicsBeginImageContext(newSize);

  // Tell the old image to draw in this new context, with the desired

  // new size

  [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

  // Get the new image from the context

  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

  // End the context

  UIGraphicsEndImageContext();

  // Return the new image.

  return newImage;

}

此方法很簡單, 但是,這種方法不是線程安全的情況下.

方法 2: 使用 CoreGraphics

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)sourceImage scaledToSize INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)newSize;

{

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGImageRef imageRef = [sourceImage CGImage];

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {

bitmapInfo = kCGImageAlphaNoneSkipLast;

}

CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {

bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}

if (sourceImage.imageOrientation == UIImageOrientationLeft) {

CGContextRotateCTM (bitmap, radians(90));

CGContextTranslateCTM (bitmap, 0, -targetHeight);

} else if (sourceImage.imageOrientation ==UIImageOrientationRight) {

CGContextRotateCTM (bitmap, radians(-90));

CGContextTranslateCTM (bitmap, -targetWidth, 0);

} else if (sourceImage.imageOrientation == UIImageOrientationUp) {

// NOTHING

} else if (sourceImage.imageOrientation == UIImageOrientationDown){

CGContextTranslateCTM (bitmap, targetWidth, targetHeight);

CGContextRotateCTM (bitmap, radians(-180.));

}

CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth,targetHeight), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage* newImage = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return newImage;

}

這種方法的好處是它是線程安全,加上它負(fù)責(zé)的 (使用正確的顏色空間和位圖信息,處理圖像方向) 的小東西,UIKit 版本不會。

如何調(diào)整和保持長寬比 (如 AspectFill 選項(xiàng))?

它是非常類似于上述,方法,它看起來像這樣:

+ (UIImage*)imageWithImage INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET UIImage*)sourceImage scaledToSizeWithSameAspectRatio INCLUDEPICTURE "http://www.61ic.com/Mobile/UploadFiles_9667/201103/20110309123315372.gif" \* MERGEFORMATINET CGSize)targetSize;

{

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if (widthFactor > heightFactor) {

scaleFactor = widthFactor; // scale to fit height

}

else {

scaleFactor = heightFactor; // scale to fit width

}

scaledWidth = width * scaleFactor;

scaledHeight = height * scaleFactor;

// center the image

if (widthFactor > heightFactor) {

thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

}

else if (widthFactor < heightFactor) {

thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

}

}

CGImageRef imageRef = [sourceImage CGImage];

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

if (bitmapInfo == kCGImageAlphaNone) {

bitmapInfo = kCGImageAlphaNoneSkipLast;

}

CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp ||sourceImage.imageOrientation == UIImageOrientationDown) {

bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {

bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth,CGImageGetBitsPerComponent(imageRef),CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}

// In the right or left cases, we need to switch scaledWidth and scaledHeight,

// and also the thumbnail point

if (sourceImage.imageOrientation == UIImageOrientationLeft) {

thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);

CGFloat oldScaledWidth = scaledWidth;

scaledWidth = scaledHeight;

scaledHeight = oldScaledWidth;

CGContextRotateCTM (bitmap, radians(90));

CGContextTranslateCTM (bitmap, 0, -targetHeight);

} else if (sourceImage.imageOrientation ==UIImageOrientationRight) {

thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);

CGFloat oldScaledWidth = scaledWidth;

scaledWidth = scaledHeight;

scaledHeight = oldScaledWidth;

CGContextRotateCTM (bitmap, radians(-90));

CGContextTranslateCTM (bitmap, -targetWidth, 0);

} else if (sourceImage.imageOrientation == UIImageOrientationUp) {

// NOTHING

} else if (sourceImage.imageOrientation == UIImageOrientationDown){

CGContextTranslateCTM (bitmap, targetWidth, targetHeight);

CGContextRotateCTM (bitmap, radians(-180.));

}

CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x,thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);

UIImage* newImage = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);

CGImageRelease(ref);

return newImage;

}

轉(zhuǎn)載于:https://www.cnblogs.com/ChouDanDan/p/5110058.html

總結(jié)

以上是生活随笔為你收集整理的UIGraphicsBeginImageContext系列知识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产小视频在线看 | 免费日b视频 | 中文字幕岛国 | 欧美一级专区 | 理论片在线观看理伦片 | 超碰在线a| 国产成人精品网站 | 亚洲在线视频一区 | 一区二区三区四区精品 | 天天操 夜夜操 | aaa黄色大片 | 成人福利午夜 | 特大黑人巨交吊性xxxxhd | 日韩国产三级 | 女王人厕视频2ⅴk | 日韩av中文字幕在线免费观看 | 实拍女处破www免费看 | av网页在线 | 九九热精品视频在线 | 老女人性视频 | 逼特逼视频在线观看 | 精品香蕉99久久久久网站 | www.久久av | 手机av免费在线 | 99久久久无码国产精品免费麻豆 | 国产盗摄av| 自拍偷拍国产 | 爽插 | 日本www网站 | 欧美午夜精品理论片 | 日韩中文字幕在线免费观看 | av免费网| 久久99久久99精品免观看粉嫩 | 成人18视频| 国产wwww| 国产精品无码久久久久成人app | 国产精品2019| 日本十八禁视频无遮挡 | www.色在线 | 欧美成人aaaaⅴ片在线看 | 久久久青草 | 成人mv| 少妇紧身牛仔裤裤啪啪 | 欧日韩在线视频 | 狠狠插影院 | 五月天在线播放 | 欧洲做受高潮欧美裸体艺术 | 国产99久一区二区三区a片 | 国产做受高潮 | 欧美视频一二三区 | 奇米一区二区 | 黄黄视频在线观看 | 久久久久久久999 | 久久成人毛片 | 日韩精品一区二区三区免费视频 | 中文字幕精品一区二 | 亚洲乱码国产乱码精品精软件 | 青青视频免费在线观看 | 香蕉视频在线网址 | 99热偷拍 | 成人网址在线观看 | a级片在线观看 | 国产香蕉尹人视频在线 | 亚洲av高清一区二区三区 | 五月天激情社区 | 欧美视频免费看欧美视频 | 中文字幕乱码在线 | 亚洲天堂无吗 | 天天干,夜夜爽 | 欧美性色19p | 在线观看网页视频 | 欧美xxxx非洲 | 天天插综合 | 日本大尺度吃奶做爰视频 | 香蕉视频黄在线观看 | 亚洲成人av电影 | 中文字幕人妻伦伦 | 无码av免费毛片一区二区 | 在线播放av片 | 亚洲美女视频在线观看 | 97在线免费公开视频 | 国产噜噜噜噜久久久久久久久 | 国产福利合集 | 羞羞色视频 | 成年人免费视频网站 | 91视频首页 | 亚洲高清视频在线播放 | 成人美女毛片 | 成人免费视频软件网站 | 午夜剧场成人 | 一本加勒比波多野结衣 | 最新亚洲精品 | 中文字幕不卡视频 | 免费三片在线视频 | 欧美一区二区三区在线观看 | 五月天激情小说 | 噼里啪啦动漫高清在线观看 | 久草视频手机在线观看 | 午夜视频免费观看 |