Save pictures to albums in Swift

Source: Internet
Author: User

本来是没有必要把这么小的一个知识点写到博客中的,但是,由于OC中的一些语法在swift中实现的时候有些特别,所以单独写下来到博客中,希望能够帮助到有需要的同学。
The wording in 1.OC

In OC, we need to call this method to save the image to the album:

void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

I think everyone has seen this method's header file, in the header file there is such a passage

// Adds a photo to the saved photos album.  The optional completionSelector should have the form://  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

To save a photo to an album, this optional completion method needs to be defined in the format of the following method.
So, in OC, we usually copy this method directly and implement this method directly, for example, a chestnut:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    UIImageWriteToSavedPhotosAlbum(self.iconView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {    if (error) {        NSLog(@"保存出错");        return;    }    NSLog(@"保存成功");}

In the above pest, I saved the image in the pre-defined IconView to the album by touching the event with my finger.

And how should the same chestnut be implemented in Swift?

The wording in 2.swift
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {    UIImageWriteToSavedPhotosAlbum(iconView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)}    func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject) {    println("---")    if didFinishSavingWithError != nil {        println("错误")        return    }    println("OK")}

The same chestnuts, implemented in swift as above, in Swift, we jump to the head of the file and it will be found to be like this,

// Adds a photo to the saved photos album.  The optional completionSelector should have the form://  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

Or OC in that set, Apple did not help us write the code of Swift under the format should be how to write, so a lot of people on this should be how to use will produce a lot of doubts, in fact, is like above, the parameter one by one corresponds to the function in Swift to write out the way you can.
Also add a little bit of knowledge:
That's the way we can write that.

    // 提示:参数 空格 参数别名: 类型func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {    println("---")//        if didFinishSavingWithError != nil {    if error != nil {        println("错误")        return    }    println("OK")}

A format like this: (parameter parameter alias: type) didfinishsavingwitherror error:nserror?
When called externally, the Didfinishsavingwitherror parameter name is displayed.
When used internally, the error parameter alias is displayed, which is convenient for us to use and more like the notation in OC.

Save pictures to albums in Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.