圆锥渐变的一种简单实现

Core Graphics 支持两种渐变:线性(Axial)和径向(Radial)渐变,但是有的时候我们可能会用到圆锥(Conical)渐变,例如在扫描附近的目标时,交互可能用上带这种渐变的雷达效果,它长这样:


要实现这样一种渐变你会怎么做呢?我的想法是从渐变的本质着手。渐变是从一种颜色渐渐变化成另外一种颜色,而圆锥渐变是根据角度渐渐变化。我们把界面看成位图,这样可以由点的位置得到它的角度,继而根据角度线性插值可以得到它的颜色,最终就可以得到圆锥渐变。

想法有了,接下来我们用它来实现上图中 Find My iPhone 图标的雷达效果吧。

首先定义一个 CALayer 的子类 ConicalLayer,

继续阅读

iOS 开发问题汇总(十)

1.在iOS 中如何使用私钥加密数据?

A:当你用私钥加密时它被称为 siging, 之后用公钥解密的过程称为 verify signature.

Why are you encrypting with the private key? When you encrypt with the private key, that is considered signing not encrypting, becuase it provides no confidentiality. If you want to “encrypt” with the private key, look into data signing, and that should allow you to “encrypt” (read “sign”) with the private key and “decrypt” (read “verify signature”) with the public key.

Reference:

Encrypting data with a private key on iOS

2.

1
[!] Due to the previous naïve CocoaPods resolver, you were using a pre-release version of `JSONModel`, without explicitly asking for a pre-release version, which now leads to a conflict. Please decide to either use that pre-release version by adding the version requirement to your Podfile (e.g. `pod 'JSONModel', '= 1.2.2P'`) or revert to a stable version by running `pod update JSONModel`.

A:今天打开一个旧的的工程,可能由于版本控制的原因,Pods 下的文件都被删除掉了,于是运行 pod install --verbose, 安装报了上面的错误。工程是一个公共库,之前都是好的,它依赖了一个私有版本的 JSONModel,这本来也没什么问题,居然报出这么一错误,挺奇怪的。按提示先显示指定版本,依旧报错,看来并不是这个原因;又运行 pod update JSONModel,有输出安装私有版本的 JSONModel 的日志,问题解决。不过不清楚为什么会出这种很诡异的问题。

3.What is the use of entitlements.plist file?

A:

Entitlements confer specific capabilities or security permissions to your iOS or macOS app.

  1. The entitlements file defines certain capabilities of your app. Usually, the file is automatically generated by Xcode when you enable a capability for your app.
  2. You only need the file if you enable certain capabilities, e.g. Healthkit integration. If you’d like to use these features, you have to add it. Otherwise, Apple will reject your app.
  3. You can name the file like you want. You can also rename it as long as the build settings point to the correct file name for it.

Reference:

What is the use of entitlements.plist file?

4.A valid provisioning profile for this executable was not found

A:Because Xcode compain about provisioning profile, so I decide to check the installed profiles on device, detail instructs are Window > Devices and Simulators > Select your device > right click > Show Provisioning Profiles…; Yes, no profile is here. It looks like we should mannual install one.

Where is the provisioning profiles Xcode managed? We can locate it by a simple way:Show project navigator > Select your project > General > Signing > Click info button follow Provisioning Profile > Drag the profile icon in popup menu to terminal. One of my profiles is : /Users/dongmeiliang/Library/MobileDevice/Provisioning\ Profiles/1923e84f-743b-4453-8876-9763596ad09b.mobileprovision , My Xcode version is 10.1 (10B61).

Reference:Refresh devices in team provisioning profile managed by Xcode 7?

iOS 中简单的图片处理

在 iOS 应用开发中,我们可能会要对图片进行旋转、缩放和裁剪,在介绍具体方法前,我们有必要先对图片做个大致的了解,这样有助于我们选择合适的方法。

图片格式

图片主要有两种格式:一种叫做位图;另一种称之为矢量图。所谓位图,就是把图片看成是由许多像素点组成;而矢量图则是用绘图指令来描述图片。举个例子,圆可以用圆点,半径,线条的粗细和颜色来描述它。从这个例子也可以看出用矢量图来描述风景,人物这样复杂的事物会比较复杂,所以它们通常会用位图来描述。

位图和矢量图也分为很多格式,具体可能查看Image file formats

下面我们讨论的是位图,在 iOS 中我们经常打交道的位图格式是 JPG 和 PNG,用来处理位图数据的类有:UIImage (UIKit),CGImage (Core Graphics) 和 CIImage (Core Image)。Image I/O 本来是属于 Core Graphics,为了更加方便使用,Apple 将它分离出来成为单独的库。

旋转

既然位图是用一个一个的像素点来模拟图片,当我们想要旋转图片时,首先想到的方法自然是改变这些像素点的位置,这当然可以达到目标。要调整这么多像素点的位置自然要耗费不少时间,所有在数码相机刚出来那会,人们不是去改变像素点的位置,而是用一段数据来描述图片的方向等信息,这段数据称为 Exif. 所以对于 JPG 这种拥有 Exif 信息的位图,我们旋转图片的最佳做法自然是改变 Exif 里的方向信息。而 PNG 是没有 Exif 信息的,所以只能改变像素点的位置。

UIImage 自带了几个可以旋转的方法:

1
2
3
4
5
6
7
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;

+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;

- (instancetype)initWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;

- (instancetype)initWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;
继续阅读