UIImageView和UIImage,CGContextRef 的一些知识点

来源:互联网 发布:2017qq协议源码 编辑:程序博客网 时间:2024/04/29 01:28
1.UIImageView不支持内部图片平铺(tile)

2.资源中的图片要用小写的,模拟器中可能不区分大小写,但在真机中区分.

   [UIImage imageNamed:@""]; 在设备中区分大小写

3.UIView没有背景图属性,有背景色属性.设置背景图可以用addSubView(backgroundImage);,推荐的是设置背景色。

4.[UIImage imageNamed:@""];它是有缓存特性

   + (UIImage *)imageWithContentsOfFile:(NSString *)path;  //does not cache the image object.

   imageNamed文档解释:

  This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

  On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of1.0. If the screen has a scale of2.0, this method first searches for an image file with the same filename with an@2xsuffix appended to it. For example, if the file’s name isbutton, it first searches forbutton@2x. If it finds a 2x, it loads that image and sets thescale property of the returnedUIImage object to2.0. Otherwise, it loads the unmodified filename and sets thescale property to1.0.

   On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

    可能在多次操作之后,应用经常发生内存警告从而导致自动退出的问题。定位之后发现是由于[UIImage imageNamed: @""]分配的图像都没有释放引起的。而之前从官方的reference中得到的信息应该是[UIImage imageNamed:@""]分配的图像系统会放到cache里面。而关于cache管理的规则就没有明确的介绍。由此看来[UIImage imageNamed:]只适合与UI界面中小的贴图的读取,而一些比较大的资源文件应该尽量避免使用这个接口。

 5. UIImage的 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

     指定两个方向上不被缩放的部分。

     对应的 rightCapWidth = image.size.width - (image.leftCapWidth + 1);

                bottomCapHeight = image.size.height - (image.topCapHeight + 1);

     leftCapWidth,rightCapWidth之间的,topCapHeight,bottomCapHeight之间的像素被平铺以达到缩放效果。

     另外UIView.contentStretch在这里好像不起作用。

      注意:使用这个函数时,要取它的返回值。并且 That method could return nil if the base image is less than 5 pixels wide or 5 pixels tall since it needs the 4 pixels for the caps + 1 pixel to stretch.

示例:设置导航栏上的自定义返回按钮

[cpp] view plaincopy
  1. - (void)setNavigationBackButton  
  2. {  
  3.     const int KLeftWidth=25;  
  4.     UIImage* image=[[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:KLeftWidth topCapHeight:0];  
  5.     UIImage* hightlightedImage=[[UIImage imageNamed:@"back_pressed.png"] stretchableImageWithLeftCapWidth:KLeftWidth topCapHeight:0];  
  6.     NSString* text=[[[self.navigationController viewControllers] objectAtIndex:0] navigationItem].title;  
  7.     UIFont* font=[Utility fontWithSize:12];  
  8.     CGSize stringSize = [text sizeWithFont:font];  
  9.     CGFloat textWidth = stringSize.width+KLeftWidth;  
  10.       
  11.     UIButton* button=[UIViewUtil createUIButtonWithImage:image  
  12.                                        hightlightedImage:hightlightedImage    
  13.                                            selectedImage:nil  
  14.                                                    title:text  
  15.                                               titleColor:color(0x33,0x33,0x33)  
  16.                                                     posX:0 posY:0];  
  17.     button.titleLabel.font=[Utility fontWithSize:12];  
  18.     CGRect rect=button.frame;  
  19.     rect.size.width=textWidth;  
  20.     button.frame=rect;  
  21.     UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];  
  22.     [button addTarget:self action:@selector(popViewControllerAnimated) forControlEvents:UIControlEventTouchUpInside];  
  23.     [[self navigationItem] setLeftBarButtonItem:buttonItem];  
  24.     [buttonItem release];  
  25.     [button release];      
  26. }  

6.支持内置动画,播放图片序列。

[cpp] view plaincopy
  1. imageView.animationImages = imageArray;  
  2. imageView.animationDuration = 0.75f;  
  3. [self.view addSubview:imageView];  
  4. [imageView startAnimating];  

7.UIImageView要设置self.userInteractionEnabled = YES才支持交互。

8. 构建图像的方式:

[cpp] view plaincopy
  1. UIGraphicsBeginImageContext(CGSizeMake(SIDE_LENGTH, SIDE_LENGTH));//创建一个新图像上下文  
  2. CGContextRef context = UIGraphicsGetCurrentContext();  
  3. ...  
  4. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();//将上下文转为一个UIImage对象。  
  5.   
  6. UIGraphicsEndImageContext();  
  7.   
  8. return theImage;  

9.动态图像,如gif,貌似 iphone不支持动态图象,采用UIWebView替代显示gif。

[cpp] view plaincopy
  1.    (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  2.   
  3. {      
  4.   
  5.         NSString* referenceURL=[info objectForKey:UIImagePickerControllerReferenceURL];  
  6.         NSLog(@"%@",referenceURL);  //assets-library://asset/asset.JPG?id=1000000001&ext=JPG  
  7.           
  8.         ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  
  9.   
  10. ...  
  11. }  
  12.   
  13. #import <AssetsLibrary/ALAssetsLibrary.h>  
  14. #import <AssetsLibrary/ALAssetRepresentation.h>  
  15.   
  16. //http://stackoverflow.com/questions/5187251/ios-select-a-gif-from-the-photo-library-convert-to-nsdata-for-use-in-multipart  

10.保存图片到相册

[cpp] view plaincopy
  1. -(void)saveToPhotosAlbum  
  2. {  
  3.     UIImage* image=nil;  
  4.     NSObject* obj=..;  
  5.     if([obj isKindOfClass:[UIImage class]])  
  6.     {  
  7.         image=(UIImage*)obj;  
  8.     }  
  9.     else  
  10.     {  
  11.         image=[UIImage imageWithData:(NSData *)obj];  
  12.     }  
  13.   
  14.     if(image)  
  15.     {  
  16.         UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:), nil);   
  17.     }  
  18.     else  
  19.     {  
  20.          //@"保存失败";  
  21.     }  
  22. }  
  23.   
  24. #if 1    
  25. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo    
  26. {    
  27.     if (error == nil)    
  28.         //@"保存成功";  
  29.     else    
  30.        //@"保存失败";  
  31. }    
  32. #endif    
11.emoji表情

emoji是日本开发的一字符编码集,在iOS中集成了该字符集, 可以通过编程的方式判断,开启或关闭该功能。

已测试它可显示于UILabel,UIButton,UIWebView,使用如“\ue415”的unicode码。

http://pukupi.com/post/1964/

可结合NSString的draw方法,把表情画到UIImage上:

[cpp] view plaincopy
  1. CGSize size = [text sizeWithFont:aFont];  
  2. if (UIGraphicsBeginImageContextWithOptions != NULL)  
  3. {  
  4.    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);  
  5. }  
  6. else  
  7. {  
  8.    UIGraphicsBeginImageContext(size);  
  9. }  
  10. [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:aFont];  
  11. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  12. UIGraphicsEndImageContext();      
  13. return image;  
编程开启iOS emoji,未测试成功。
http://blog.csdn.net/favormm/article/details/6774899


iOS5.1下emoji表情显示方框的解决办法   

在iOS5.1的部分设备上,emoji表情无法正常显示.原因是iOS4上面的emoji用的是softbank的编码,到iOS5以后,emoji被放进了Unicode6.0,导致原来的老编码可能存在部分不兼容现象.
解决办法在iOS5上面全部用新编码,在iOS4及以下全部用老编码.
因为有些iOS5.1上可以正常显示,有些不行。根据我们的测试情况,5.x的全部用新编码,4.x及以下全部用老编码就没问题了
苹果自己的转换表: http://opensource.apple.com/source/ICU/ICU-461.13/icuSources/data/translit/Any_SoftbankSMS.txt 左边的是Unicode新编码,右边是softbank的老编码,请自行转换

http://www.cocoachina.com/bbs/read.php?tid=96847&page=1

12.UIImagePickerController 拍照后,图片旋转了90度。可能这个情况在5.0就不出现了。

[cpp] view plaincopy
  1. #pragma mark UIImagePickerControllerDelegate  
  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  3. {  
  4.     [picker dismissModalViewControllerAnimated:YES];  
  5.       
  6.     NSString* mediaType=[info objectForKey:UIImagePickerControllerMediaType];  
  7.     if([mediaType isEqualToString:(NSString*)kUTTypeImage])//@"public.image"  
  8.     {  
  9.         UIImage* image=[info objectForKey:UIImagePickerControllerOriginalImage];  
  10.         UIImageOrientation imageOrientation=image.imageOrientation;  
  11.         if(imageOrientation!=UIImageOrientationUp)  
  12.         {  
  13.             // 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。  
  14.             // 以下为调整图片角度的部分  
  15.             UIGraphicsBeginImageContext(image.size);  
  16.             [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];  
  17.             iPortraitImageView.image = UIGraphicsGetImageFromCurrentImageContext();  
  18.             UIGraphicsEndImageContext();  
  19.             // 调整图片角度完毕  
  20.         }  
  21.     }  
  22. }    
  23.   
  24. //设置可编辑选取的图片  
  25.   
  26. UIImagePickerController* pickerController = [[UIImagePickerController alloc] init];  
  27.         pickerController.delegate = self;  
  28.         pickerController.allowsEditing=YES;//设置可编辑选取的图片  
  29.   
  30.         if(buttonIndex==0)  
  31.         {  
  32.             if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])  
  33.             {  
  34.                 pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  35.             }  
  36.         }  
  37.         else  
  38.         {  
  39.             if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  
  40.             {  
  41.                 pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;  
  42.             }  
  43.         }  
  44.           
  45.         [[UIViewUtil firstAvailableUIViewControllerWithView:self] presentModalViewController:pickerController animated:YES];  
  46.         [pickerController release];  
  47.   
  48. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  49. {  
  50.     [picker dismissModalViewControllerAnimated:YES];  
  51.       
  52.     NSString* mediaType=[info objectForKey:UIImagePickerControllerMediaType];  
  53.     if([mediaType isEqualToString:(NSString*)kUTTypeImage])//@"public.image"  
  54.     {  
  55.         UIImage* image=[info objectForKey:UIImagePickerControllerEditedImage];  
  56.         iPortraitImageView.image=image;  
  57.     }  
  58. }   
  59.   
  60. 后面再次测试,结果测试每次拍照:  
  61.   
  62. UIImage* image=[info objectForKey:UIImagePickerControllerOriginalImage];  
  63. UIImageOrientation imageOrientation=image.imageOrientation;  
  64.   
  65. imageOrientation的值如左列,拍照时home键在位置为右列的值  
  66.   
  67. UIImageOrientationRight 下  
  68. UIImageOrientationUp    右  
  69. UIImageOrientationDown  左  
  70. UIImageOrientationLeft  上  

若拍照多次后,出现黑屏,检查是否有打印内容警告,同时看是否有上一次的拍照的图片没释放。

13. 对nil的image.size.width取值,结果可能不确定。可能在大多数机器上取值为0,而有的机器上取到的是不确定值。

      所以一定要判断如果为nil,width取值0。

14.用图片填充backgroundColor时,图片上的透明区域可能为成为黑色。backgroundColor是随内容大小而铺的。

 

15.在资源目录中,如果同时存在Icon.png 114x114像素,Icon@2x.png 114x114像素

[java] view plaincopy
  1. [image imageNamed:@"Icon.png"]; //尺寸是114点,使用的图片是Icon.png,像素/点为1:1  
  2.   
  3. NSString* path=[[NSBundle mainBundle ] pathForResource:@"Icon" ofType:@"png"];  
  4. [UIImage imageWithContentsOfFile:path];//尺寸是114点,使用的图片是Icon.png,像素/点为1:1  
  5.   
  6. path=[[NSBundle mainBundle ] pathForResource:@"Icon@2x" ofType:@"png"];  
  7. UIImage imageWithContentsOfFile:path];//尺寸是57点,使用的图片是Icon@2x.png,像素/点为2:1  

在Documents中时,imageWithContentsOfFile的结果同上。

看样子,对文件名中的@2x的识别,是UIImage在根据路径找图片后本身完成的,并且它是看最后使用的图片名,而不一定是在路径中指定的。

[java] view plaincopy
  1. data=[NSData dataWithContentsOfFile:path];  
  2. image=[UIImage imageWithData:data];对于Icon.png和Icon@2x.png,尺寸都是114点,像素/点为1:1。  

16.图片圆角化

     http://www.cocoachina.com/bbs/read.php?tid=1757&page=1

17. 给图片加滤镜

      用Core Graphic的API,把图片解析成RGBA四通道的位图放入内存, 然后内存中有一个数组,数组中的每四个元素都是图像上的一个像素点的RGBA的数值(0-255),改变RGB的数值,再写回去重新生成。

      变为黑白照片就是把每个像素点的RGB的值相加求平均值,再回写回去。例如:R=B=G=100,就是灰色的,写 for循环把每个像素点的RGB都改成各自的平均值,照片就变为黑白色了。如果图像变为怀旧照片,就是底色发黄的,就是RG的比值调高,B保持不变,因为红绿相配就是黄色。
      借助Android里的ColorMatrix(颜色矩阵)的概念,重要的就是把每个像素点的RGB调整为新值。

      http://www.cocoachina.com/bbs/read.php?tid=69525

      http://www.cnblogs.com/leon19870907/articles/1978065.html

18.   绘半透明矩形
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(ctx,[[UIColor blackColor] colorWithAlphaComponent:0.5].CGColor);
        CGContextAddRect(ctx, CGRectMake(0, 0, 320, 44));
        CGContextClosePath(ctx);
        CGContextDrawPath(ctx, kCGPathFill);


19.  GPUImage

       https://github.com/BradLarson/GPUImage

       先编译framework目录下GPUImage.xcodeproj,生成.a,再编译examples下某个应用程序。
原创粉丝点击