首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >self.bounds混淆

self.bounds混淆
EN

Stack Overflow用户
提问于 2012-02-27 13:19:12
回答 1查看 3.8K关注 0票数 1

在这个代码中

CGRect contextRect = self.bounds;

会指哪一条路?矩形、imageRect或整个iOS视图。

我试图使用quartz2D操纵图像,我通过查看不同的示例创建了这段代码,并且编写在// is之间的代码来自苹果Quartz2D指南绘制文本。

先谢谢你,问候。

代码语言:javascript
复制
    - (void)drawRect:(CGRect)rect
    {
        CGSize cgs = CGSizeMake(250.0, 400.0);
        UIGraphicsBeginImageContext(cgs);

        CGRect rectangle = CGRectMake(0,0,cgs.width,cgs.height);
        CGRect imageRect = CGRectInset(rectangle, 5.4, 5.4);
        imageRect.size.height -= 100;

        UIImage *myImage = [UIImage imageNamed:@"pumpkin.jpg"]; 
        [myImage drawInRect:imageRect];    

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 10.0);
        CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
        CGContextStrokeRect(context, rectangle);

        //    

        CGRect contextRect = self.bounds;

        CGContextTranslateCTM(context, 0, contextRect.size.height);
        CGContextScaleCTM(context, 1, -1);

        float w, h;
        w = contextRect.size.width;
        h = contextRect.size.height;

        CGAffineTransform myTextTransform;
        CGContextSelectFont (context, "Helvetica-Bold", h/10, kCGEncodingMacRoman);
        CGContextSetCharacterSpacing (context, 10);
        CGContextSetTextDrawingMode (context, kCGTextFillStroke);

        CGContextSetRGBFillColor(context, 0, 1, 0, .5);
        CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
        myTextTransform =  CGAffineTransformMakeRotation(0);
        CGContextSetTextMatrix(context, myTextTransform);
        CGContextShowTextAtPoint(context, 0, 50, "Quartz 2D", 9);

        //    

        UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [testImg drawAtPoint:CGPointMake(35, 10)];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-27 13:24:51

self.frame表示视图在其父视图坐标系中的同弦和大小。

self.bounds表示视图在其自身坐标系中的坐标和大小。因此,它的widthheight总是与self.frame相同,但它的xy等于0

self.bounds等于CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)

所以你的代码:

代码语言:javascript
复制
CGRect contextRect = self.bounds;

与以下相同:

代码语言:javascript
复制
CGRect contextRect = rect;
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9465675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档