iOS 绘图学习笔记 - (2)图像中的几何问题

Points vs Pixels

iOS中Points表示屏幕或者绘画中的逻辑点, 我们在绘画以及设置控件坐标等等操作,都是使用的Point作为单位的. Pixel是一个物理像素, 是颜色和亮度的最小的承载单位.

Scale

iOS中为了标明 Point 和pixel的换算关系, 在UIScreen中有一个scale属性来标注.

View 坐标系

iOS中View的坐标系是top-left的坐标系.

Frames 和 bounds

Views 有两个重要的坐标系,frame 表示view在父view的坐标, bounds表示view在自己的坐标系中的位置(改变bounds可以用来展示view自己的某一块区域, scrollView).

不同坐标系之间的转换

iOS的UIView提供了不同View之间坐标系的转换方法:

CGPoint convertedPoint = [outerView convertPoint:samplePoint fromView:grayView];

其中,这里转换的过程是从 fromView的坐标系 -> toView的坐标系

重要的数据结构

最重要的是Transform -> 用来表示从一个坐标系到另外一个坐标系的映射关系.

将这些变量转化成Objects

strings

CGPoint - NSStringfromCGPoint() - CGPointFromString
CGSize - NSStringFromCGSize() - CGSizeFromString
CGRect - NSStringFromCGRect() - CGRectFromtString
CGAffineTransfrom - NSStringFromAffineTransform() - CGAffineTransformString()

Dictionary

通常CGPoint, CGSize, CGRect,CGAffineTransform都是用CFDictionaryRef来bridge数据.

Type - Convert to Dict - Convert from Dict
CGPoint - CGPointCreateDictionaryRepresentation - CGPointMakeWithDictionaryRepresentation
CGSize - CGSizeCreateDictionaryRepresentation - CGSizeMakeWithDictionaryRepresentation
CGRect - CGRectCreateDictionaryRepresentation - CGRectMakeWithDictionaryRepresentation

// Convert CGRect to NSDictionary representation
CGRect testRect = CGRectMake(1, 2, 3, 4);
NSDictionary *dict = (__bridge_transfer NSDictionary *)CGRectCreateDictionaryRepresentation(testRect);
NSLog(@"Dictionary: %@", dict);

// Convert NSDictionary representation to CGRect
CGRect outputRect;
BOOL success = CGRectMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)dict, &outputRect);
if (success) {
    NSLog(@"Rect: %@", NSStringFromCGRect(outputRect));
}
Values

iOS中使用NSValue可以提供一个C data的包装类(所有的c的数据条目都可以用它进行包装).

Values are most useful for adding structures to arrays and dictionaries for in-app algorithms.

CGPoint - +valueWithCGPoint: - CGPointValue
CGSize - +valueWithCGSize: - CGSizeValue
CGRect - +valueWithCGRect: - CGRectValue
CGAffineTransform - +valueWithCGAffineTransform - CGAffineTransfromValue

Rectangle便利方法

CGRect RectMakeRect(CGPoint origin, CGSize size) {
    return (CGRect){.origin = origin, .size = size};
}

CGPoint RectGetCenter(CGRect rect) {
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}

Fitting 和 Filling

当我们适配一个图片到另外一个或大或小的空间时候, 我们需要resize这个绘图, 因为我们需要计算一个targetSize去拉伸或缩放. 有四种常见的resize方法: centering, fitting, filling, and squeezing.

这四种方式和View的contentMode息息相关

Centering

这里使用的原始图片使用原始scale, 但是直接在目的地的center中心展示. 因此如果原图过大, 会被crop, 如果太小, 周边会留白.

Fitting

Fitting 原始图片到目的target, 因此需要修改scale (aspect ratio不变), 这个scale值由以下方法确定: 水平 或者 垂直贴合.

// Multiply the size components by the factor
CGSize SizeScaleByFactor(CGSize aSize, CGFloat factor) {
    return CGSizeMake(aSize.width * factor, aSize.height * factor);
}

// Calculate scale for fitting a size to a destination CGFloat AspectScaleFit(CGSize sourceSize, CGRect destRect) {
    CGSize destSize = destRect.size;
    CGFloat scaleW = destSize.width / sourceSize.width; 
    CGFloat scaleH = destSize.height / sourceSize.height;
    return MIN(scaleW, scaleH);
}

// Return a rect fitting a source to a destination CGRect RectByFittingInRect(CGRect sourceRect, CGRect destinationRect) {
    CGFloat aspect = AspectScaleFit(sourceRect.size, destinationRect);
    CGSize targetSize = SizeScaleByFactor(sourceRect.size, aspect);
    return RectAroundCenter( RectGetCenter(destinationRect), targetSize);
}
Filling

Filling 同Fitting也是要求至少有一边贴合, 但是另一个需求是完全覆盖.ensures that every pixel of the destination space corresponds to the drawing area within the source image.

// Calculate scale for filling a destination
CGFloat AspectScaleFill(CGSize sourceSize, CGRect destRect) {
    CGSize destSize = destRect.size;
    CGFloat scaleW = destSize.width / sourceSize.width; 
    CGFloat scaleH = destSize.height / sourceSize.height;
    return MAX(scaleW, scaleH);
}

// Return a rect that fills the destination
CGRect RectByFillingRect(CGRect sourceRect, CGRect destinationRect){
    CGFloat aspect = AspectScaleFill(sourceRect.size, destinationRect);
    CGSize targetSize = SizeScaleByFactor(sourceRect.size, aspect);
    return RectAroundCenter(RectGetCenter(destinationRect), targetSize);
}
squeezing

不常用.不能 aspect ratio.

你可能感兴趣的:(iOS 绘图学习笔记 - (2)图像中的几何问题)