2017-01-27 11 views
0

私はSwiftとプログラミングで新しく、ColorPicker用に表示する画像を生成したいと思います。SwiftColorPickerを使用しています。画像作成用に次のコードがあります。ColorPickerのためのUIImageを生成する(CGDataProviderおよびCGImageSource)

private func createImageFromData(_ width:Int, height:Int) { 
    let colorSpace = CGColorSpaceCreateDeviceRGB() 
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue) 
    provider = CGDataProvider(data: mutableData) 
    imageSource = CGImageSourceCreateWithDataProvider(provider, nil) 
    let cgImage = CGImage.init(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: width * 4, space: colorSpace, bitmapInfo: bitmapInfo, provider: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) 

    if let cgimg = cgImage { 
     image = UIImage(cgImage: cgimg) 

      PHPhotoLibrary.shared().performChanges({ 
       PHAssetChangeRequest.creationRequestForAsset(from: self.image!) 
      }, completionHandler: { success, error in 
       if success { 
        print("succes") 
       } 
       else if let error = error { 
        print("error") 
        debugPrint(error as Any) 
       } 
       else { 
        print("woooot?") 
       } 
      }) 


    } else { print("Where the hell is the image?") } 
} 


func changeSize(_ width:Int, height:Int) { 
    self.width = width 
    self.height = height 
    let size:Int = width * height * 4 
    CFDataSetLength(mutableData, size) 
    createImageFromData(width, height: height) 
} 

init(width:Int, height:Int) { 
    self.width = width 
    self.height = height 
    let size:Int = width * height * 4 
    mutableData = CFDataCreateMutable(kCFAllocatorDefault, size) 
    createImageFromData(width, height: height) 
} 

public func writeColorData(_ h:CGFloat, a:CGFloat) { 

    let d = CFDataGetMutableBytePtr(self.mutableData) 

    if width == 0 || height == 0 { 
     return 
    } 

    var i:Int = 0 
    let h360:CGFloat = ((h == 1 ? 0 : h) * 360)/60.0 
    let sector:Int = Int(floor(h360)) 
    let f:CGFloat = h360 - CGFloat(sector) 
    let f1:CGFloat = 1.0 - f 
    var p:CGFloat = 0.0 
    var q:CGFloat = 0.0 
    var t:CGFloat = 0.0 
    let sd:CGFloat = 1.0/CGFloat(width) 
    let vd:CGFloat = 1/CGFloat(height) 

    var double_s:CGFloat = 0 
    var pf:CGFloat = 0 
    let v_range = 0..<height 
    let s_range = 0..<width 

    for v in v_range { 
     pf = 255 * CGFloat(v) * vd 
     for s in s_range { 
      i = (v * width + s) * 4 
      d?[i] = UInt8(255) 
      if s == 0 { 
       q = pf 
       d?[i+1] = UInt8(q) 
       d?[i+2] = UInt8(q) 
       d?[i+3] = UInt8(q) 
       continue 
      } 

      double_s = CGFloat(s) * sd 
      p = pf * (1.0 - double_s) 
      q = pf * (1.0 - double_s * f) 
      t = pf * (1.0 - double_s * f1) 
      switch(sector) { 
      case 0: 
       d?[i+1] = UInt8(pf) 
       d?[i+2] = UInt8(t) 
       d?[i+3] = UInt8(p) 
      case 1: 
       d?[i+1] = UInt8(q) 
       d?[i+2] = UInt8(pf) 
       d?[i+3] = UInt8(p) 
      case 2: 
       d?[i+1] = UInt8(p) 
       d?[i+2] = UInt8(pf) 
       d?[i+3] = UInt8(t) 
      case 3: 
       d?[i+1] = UInt8(p) 
       d?[i+2] = UInt8(q) 
       d?[i+3] = UInt8(pf) 
      case 4: 
       d?[i+1] = UInt8(t) 
       d?[i+2] = UInt8(p) 
       d?[i+3] = UInt8(pf) 
      default: 
       d?[i+1] = UInt8(pf) 
       d?[i+2] = UInt8(p) 
       d?[i+3] = UInt8(q) 
      } 


     } 
    } 
} 

と:

// Write colors to data array 
     if self.data1Shown { self.pickerImage2!.writeColorData(self.h, a:self.a) } 
     else { self.pickerImage1!.writeColorData(self.h, a:self.a)} 

を持つ:それが失敗した

public var a:CGFloat = 1 { 
    didSet { 
     if a < 0 || a > 1 { 
      a = max(0, min(1, a)) 
     } 
    } 
} 

public var h:CGFloat = 0 { // // [0,1] 
    didSet { 
     if h > 1 || h < 0 { 
      h = max(0, min(1, h)) 
     } 
     renderBitmap() 
     setNeedsDisplay() 
    } 

} 

私は写真アルバムに保存しようとすると(私は画像を設定することができなかったので、それを行ったので、それが大丈夫かエラーかを理解するために保存しようとしたため)、そして、優雅なエラーが表示されます:

エラードメイン= NSCocoaErrorDomainコード= -1「(ヌル)」

だから私は多分誰かが何をすべきかを知っている知りたい、私はこの問題を解決する方法を、ここのためのgithubの上のリンクでありますソース(多分それは役立ちます):

https://github.com/MrMatthias/SwiftColorPicker

ありがとう!

+0

この回答はすでにあなたが探しているものだと思います:http://stackoverflow.com/a/993159/1718685 – Stephen

答えて

0

便利な色のUIImageを作成するための便利な拡張機能を作成しました。

それを使用してお気軽に:

extension UIImage { 

    static func image(of color: UIColor, size: CGSize) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 

     color.set() 
     UIRectFill(CGRect(origin: CGPoint.zero, size: size)) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image! 
    } 

} 

は、その後、あなたがして使用することができます役立ちます

let redImage = UIImage.image(of: .red, size: CGSize(width: 200, height: 200)) 

希望を!

関連する問題