美文网首页
Swift图片处理

Swift图片处理

作者: 飞扬跋扈的野草 | 来源:发表于2016-11-30 14:41 被阅读233次

本文源码在Github: ***

简单图片背景替换(实现抠图效果)

参考:
http://blog.csdn.net/hitwhylz/article/details/41550411?utm_source=tuicool&utm_medium=referral
http://www.hangge.com/blog/cache/detail_904.html
http://www.jianshu.com/p/266f796d61d3

在网上搜索“Swift扣图”文章较少,目前能找到的只有以上几位同学的分享,分别借鉴一下。
尝试以上方案出现以下错误:

duplicate symbol _createCubeMap in: 
/Users/nathan/Library/Developer/Xcode/DerivedData/Koutu2-eplaxzssfxopryhhxjukijapxifp/Build/Intermediates/Koutu2.build/Debug-iphonesimulator/Koutu2.build/Objects-normal/x86_64/CubeMap.o 
/Users/nathan/Library/Developer/Xcode/DerivedData/Koutu2-eplaxzssfxopryhhxjukijapxifp/Build/Intermediates/Koutu2.build/Debug-iphonesimulator/Koutu2.build/Objects-normal/x86_64/ViewController.o 
ld: 1 duplicate symbol for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

解决不了~~~

晚上试下这个 https://github.com/zhangao0086/iOS-CoreImage-Swift/tree/master/ComplexFilters/ComplexFilters
还是一样的问题


怎么整??????


Swift开发中常用的一些图片处理方法

来源:http://www.jianshu.com/p/0402e470e8c3

//MARK: - 生成指定尺寸的纯色图片
func imageWithColor(color: UIColor!, size: CGSize) -> UIImage{
    var size = size
    if CGSizeEqualToSize(size, CGSizeZero){
        size = CGSizeMake(1, 1)
    }
    let rect = CGRectMake(0, 0, size.width, size.height)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

//MARK: - 修改图片尺寸
func imageScaleToSize(image: UIImage, size: CGSize) -> UIImage{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    // Determine whether the screen is retina
    if UIScreen.mainScreen().scale == 2.0{
        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)
    }else{
        UIGraphicsBeginImageContext(size)
    }

    // 绘制改变大小的图片
    image.drawInRect(CGRectMake(0, 0, size.width, size.height))

    // 从当前context中创建一个改变大小后的图片
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()

    // 使当前的context出堆栈
    UIGraphicsEndImageContext()

    // 返回新的改变大小后的图片
    return scaledImage
}

//MARK: - 压缩图片大小
func imageCompress(originalImage: UIImage) -> UIImage{
    guard let imageData = UIImageJPEGRepresentation(originalImage, 0.5) else{
        return originalImage
    }

    let compressImage = UIImage(data: imageData)!
    return compressImage
}

相关文章

网友评论

      本文标题:Swift图片处理

      本文链接:https://www.haomeiwen.com/subject/lsltmttx.html