`

Objective-C 图像

阅读更多

一.UIImage类是用来处理图像,比如在窗口上显示图像,从一个文件上读取图像等等。

    1.UIImage提供了一下不同的方法来读取图片,从而创建图像对象;

       //从应用程序上读取图像,参数为文件名(文件已经拷贝在应用中)

       +【UIImage imageNamed:(NSString *) name】

       //从文件系统上读取图像,参数为完整文件名(包括路径信息)

       -  [UIImage initWithContentsOfFile:(NSString *)path]

       -  [UIImage initWithData:(NSData *)data]//从内存中读取图像数据

         除了读取图像外,UIImage还可以生成一个bitmap图像。比如,用户在某一个应用程序的窗口上画了一个图,然后 应用程序可以将该图保存成一个bitmap图像。

         -(UIImage *)saveToImage:(CGSize)size{

              UIImage *result=nil;

              UIGraphicsBeginImageContext(size);

             //调用画图的代码

             result=UIGraphicsGetImageFromCurrentContext();//捕捉所化的图

             UIGraphicsEndImageContext();

            return result; 

 

       } 

      另外,你可以使用如下方法将图像转换成PNG和JPEG

       NSData  *UIImagePNGRepresentation(UIImage *image);

       NSData  *UIImageJPGRepresentation(UIImage *image);

      UIImages也提供了drawRect方法,所以,开发人员可以调用下述方法画图:

  

      -【UIImage drawAtPoint:(CGPoint)point】

      -【UIImage drawAtPoint:(CGPoint)point】

      -【UIImage drawAsPatternInRect :(CGRect)rect】

二.图像的滚动.放大和缩小

     在创建视图时,你可以在界面创建器上设置UIScrollView的大小。

     CGRect  fram =CGRectMake(0,0,200,200);

     scrollView =[[UIScrollView alloc] initWithFrame:frame];

     首先创建图像视图,然后添加该图像视图到滚动视图下。最后设置滚动视图的大小为图像视图的大小:

     frame=CGRectMake(0,0,500,500);

     myImageView=【【UIImageView alloc】initWithFrame:frame】

    【scrollView addSubview:myImageView];

     scollView.contetSize=CGSizeMake(500,500);

     在iphone中是通过UIScrollViewDelegate来完成。

     初了滚动,UIScrollView也支持放大和缩小,比如:

      scrollView.maximumZoomScale=2.0;

      scrollView.minimumZoomScale=scrollView.size.width/myImage.size.width;

      

     

    

 

 

 

    

       

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics