Photos 框架的基本使用

从 iOS 9 开始 Apple 把 Asset Library 标记为废弃状态,并建议开发者使用 Photos 框架。

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library.

不幸的是 Apple 并没有发布相关的使用指导文档,只有一个相关 Demo。使用的时候固然可以回头参考这个 Demo,但这样的效率不是很高,很多概念也容易忘记,所以这里做个简单的总结。

Photos 中有不少类,其中几个犹为关键。PHPhotoLibary 是我们操作 Photo Library 里面资源的入口对象,所有的操作都通过它完成。PHCollectionList 表示相册中的专题列表;PHAssetCollection 表示专题;PHAsset 表示资源,如 images, videos, and Live Photos.

我们基本的需求就是 CRUD, 这些操作是需要用户授权的,记得先获取权限再操作, 下面我们展示相关的代码片段。

Create

  1. 创建一个资源
1
2
3
4
5
PHPhotoLibrary.shared().performChanges({
            PHAssetChangeRequest.creationRequestForAsset(from: image)
        }, completionHandler: {success, error in
            if !success { print("error creating asset: \(error)") }
        })
  1. 创建一个资源到指定的专题
1
2
3
4
5
6
7
8
9
PHPhotoLibrary.shared().performChanges({
            let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
            if let assetCollection = self.assetCollection {
            let addAssetRequest = PHAssetCollectionChangeRequest(for: assetCollection)
            addAssetRequest?.addAssets([creationRequest.placeholderForCreatedAsset!] as NSArray)
            }
        }, completionHandler: {success, error in
            if !success { print("error creating asset: \(error)") }
        })
继续阅读

iOS 开发问题汇总(九)

1.Curried functions in Swift

A:There’s a difference between self.methodname (which you are using), and Classname.methodname.

The former, when called within a class’s method, will give you a function bound with that class instance. So if you call it, it will be called on that instance.

The latter gives you a curried function that takes as an argument any instance of Classname, and returns a new function that is bound to that instance. At this point, that function is like the first case (only you can bind it to any instance you like).

Here’s an example to try and show that a bit better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class C {
    private let _msg: String
        init(msg: String) { _msg = msg }

    func c_print() { print(_msg) }

    func getPrinter() -> ()->() { return self.c_print }
}

let c = C(msg: "woo-hoo")
let f = c.getPrinter()
// f is of type (())->()
f() // prints "woo-hoo"

let d = C(msg: "way-hey")

let g = C.c_print
// g is of type (C) -> (()) -> (),
// you need to feed it a C:
g(c)() // prints "woo-hoo"
g(d)() // prints "way-hey"

// instead of calling immediately,
// you could store the return of g:
let h = g(c)
// at this point, f and h amount to the same thing:
// h is of type (())->()
h() // prints "woo-hoo"

Reference:Curried functions in SWIFT

2.NSLog on devices in iOS 10 / Xcode 8 will truncate.

A:A temporary solution, just redefine all NSLOG to printf in a global header file.

1
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

Reference:NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

继续阅读

音频和视频格式

音频格式

我们通常说的音频格式准确地讲应该是音频文件格式,它是计算机系统上用于存放数字音频数据的文件格式,也可以看作一个容器。

音频数据的比特分布我们称为音频编码格式,它可以非压缩编码或压缩编码。压缩编码又分为无损压缩和有损压缩。

编码器(codec)就是来编解码原始音频数据的。

声音源 --ADC--> raw audio data --codec--> audio data(uncompressed/compressed) --packed--> audio file format(container format)

An audio file format is a file format for storing digital audio data on a computer system. The bit layout of the audio data (excluding metadata) is called the audio coding format and can be uncompressed, or compressed to reduce the file size, often using lossy compression. The data can be a raw bitstream in an audio coding format, but it is usually embedded in a container format or an audio data format with defined storage layer.

It is important to distinguish between the audio coding format, the container containing the raw audio data, and an audio codec. A codec performs the encoding and decoding of the raw audio data while this encoded data is (usually) stored in a container file. Although most audio file formats support only one type of audio coding data (created with an audio coder), a multimedia container format (as Matroska or AVI) may support multiple types of audio and video data.

There are three major groups of audio file formats:

• Uncompressed audio formats, such as WAV, AIFF, AU or raw header-less PCM;

• Formats with lossless compression, such as FLAC, Monkey’s Audio (filename extension .ape), WavPack (filename extension .wv), TTA, ATRAC Advanced Lossless, ALAC (filename extension .m4a), MPEG-4 SLS, MPEG-4 ALS, MPEG-4 DST, Windows Media Audio Lossless (WMA Lossless), and Shorten (SHN).

• Formats with lossy compression, such as Opus, MP3, Vorbis, Musepack, AAC, ATRAC and Windows Media Audio Lossy (WMA lossy).

继续阅读