15 changed files with 898 additions and 176 deletions
-
3Podfile
-
32kplayer.xcodeproj/project.pbxproj
-
58kplayer/Base.lproj/Main.storyboard
-
245kplayer/DetailViewController.swift
-
17kplayer/ItemCell.swift
-
1kplayer/MasterViewController.swift
-
231kplayer/VideoPlayerController.swift
-
59kplayer/core/ImageLoadOperation.swift
-
16kplayer/core/MediaItem.swift
-
54kplayer/core/NetData.swift
-
59kplayer/core/NetworkHelper.swift
-
146kplayer/core/NetworkManager.swift
-
38kplayer/core/UploadOperation.swift
-
48kplayer/core/alamojson.swift
-
27kplayer/util/UIImageExtension.swift
@ -0,0 +1,59 @@ |
|||||
|
// |
||||
|
// Created by Marco Schmickler on 21.03.15. |
||||
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved. |
||||
|
// |
||||
|
|
||||
|
import Foundation |
||||
|
import Alamofire |
||||
|
import SwiftyJSON |
||||
|
|
||||
|
class ImageLoadOperation: NSOperation { |
||||
|
let baseUrl: String |
||||
|
let item: MediaItem |
||||
|
|
||||
|
init(baseUrl: String, item: MediaItem) { |
||||
|
self.baseUrl = baseUrl |
||||
|
self.item = item |
||||
|
} |
||||
|
|
||||
|
override func main() { |
||||
|
if self.cancelled { |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
let path = baseUrl + "/service/download" + item.thumbUrl! |
||||
|
println(path) |
||||
|
|
||||
|
Alamofire.request(.GET, path).validate().responseImage( { |
||||
|
(_, _, image, error) in |
||||
|
|
||||
|
if error == nil && image != nil { |
||||
|
self.item.image = image |
||||
|
} |
||||
|
else { |
||||
|
println(error) |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
extension Alamofire.Request { |
||||
|
class func imageResponseSerializer() -> Serializer { |
||||
|
return { request, response, data in |
||||
|
if data == nil { |
||||
|
return (nil, nil) |
||||
|
} |
||||
|
|
||||
|
let image = UIImage(data: data!) //, scale: UIScreen.mainScreen().scale) |
||||
|
|
||||
|
return (image, nil) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func responseImage(completionHandler: (NSURLRequest, NSHTTPURLResponse?, UIImage?, NSError?) -> Void) -> Self { |
||||
|
return response(serializer: Request.imageResponseSerializer(), completionHandler: { (request, response, image, error) in |
||||
|
completionHandler(request, response, image as? UIImage, error) |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
// |
||||
|
// NetData.swift |
||||
|
// Net |
||||
|
// |
||||
|
// Created by Le Van Nghia on 8/3/14. |
||||
|
// Copyright (c) 2014 Le Van Nghia. All rights reserved. |
||||
|
// |
||||
|
import Foundation |
||||
|
import UIKit |
||||
|
enum MimeType: String { |
||||
|
case ImageJpeg = "image/jpeg" |
||||
|
case ImagePng = "image/png" |
||||
|
case ImageGif = "image/gif" |
||||
|
case Json = "application/json" |
||||
|
case Unknown = "" |
||||
|
func getString() -> String? { |
||||
|
switch self { |
||||
|
case .ImagePng: |
||||
|
fallthrough |
||||
|
case .ImageJpeg: |
||||
|
fallthrough |
||||
|
case .ImageGif: |
||||
|
fallthrough |
||||
|
case .Json: |
||||
|
return self.rawValue |
||||
|
case .Unknown: |
||||
|
fallthrough |
||||
|
default: |
||||
|
return nil |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class NetData |
||||
|
{ |
||||
|
let data: NSData |
||||
|
let mimeType: MimeType |
||||
|
let filename: String |
||||
|
init(data: NSData, mimeType: MimeType, filename: String) { |
||||
|
self.data = data |
||||
|
self.mimeType = mimeType |
||||
|
self.filename = filename |
||||
|
} |
||||
|
init(pngImage: UIImage, filename: String) { |
||||
|
data = UIImagePNGRepresentation(pngImage) |
||||
|
self.mimeType = MimeType.ImagePng |
||||
|
self.filename = filename |
||||
|
} |
||||
|
init(jpegImage: UIImage, compressionQuanlity: CGFloat, filename: String) { |
||||
|
data = UIImageJPEGRepresentation(jpegImage, compressionQuanlity) |
||||
|
self.mimeType = MimeType.ImageJpeg |
||||
|
self.filename = filename |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
// |
||||
|
// Created by Marco Schmickler on 22.03.15. |
||||
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved. |
||||
|
// |
||||
|
|
||||
|
import Foundation |
||||
|
import Alamofire |
||||
|
|
||||
|
func urlRequestWithComponents(urlString: String, parameters: NSDictionary) -> (URLRequestConvertible, NSData) { |
||||
|
|
||||
|
// create url request to send |
||||
|
var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!) |
||||
|
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue |
||||
|
//let boundaryConstant = "myRandomBoundary12345" |
||||
|
let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())" |
||||
|
let contentType = "multipart/form-data;boundary=" + boundaryConstant |
||||
|
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type") |
||||
|
|
||||
|
|
||||
|
// create upload data to send |
||||
|
let uploadData = NSMutableData() |
||||
|
|
||||
|
// add parameters |
||||
|
for (key, value) in parameters { |
||||
|
|
||||
|
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) |
||||
|
|
||||
|
if value is NetData { |
||||
|
// add image |
||||
|
var postData = value as! NetData |
||||
|
|
||||
|
//uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) |
||||
|
|
||||
|
// append content disposition |
||||
|
var filenameClause = " filename=\"\(postData.filename)\"" |
||||
|
let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n" |
||||
|
let contentDispositionData = contentDispositionString.dataUsingEncoding(NSUTF8StringEncoding) |
||||
|
uploadData.appendData(contentDispositionData!) |
||||
|
|
||||
|
|
||||
|
// append content type |
||||
|
//uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) // mark this. |
||||
|
let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n" |
||||
|
let contentTypeData = contentTypeString.dataUsingEncoding(NSUTF8StringEncoding) |
||||
|
uploadData.appendData(contentTypeData!) |
||||
|
uploadData.appendData(postData.data) |
||||
|
|
||||
|
} else { |
||||
|
uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!) |
||||
|
} |
||||
|
} |
||||
|
uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) |
||||
|
|
||||
|
// return URLRequestConvertible and NSData |
||||
|
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData) |
||||
|
} |
||||
|
|
||||
|
class NetworkHelper { |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
// |
||||
|
// Created by Marco Schmickler on 21.03.15. |
||||
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved. |
||||
|
// |
||||
|
|
||||
|
import Foundation |
||||
|
import Alamofire |
||||
|
import SwiftyJSON |
||||
|
|
||||
|
class UploadOperation: NSOperation { |
||||
|
let baseUrl: String |
||||
|
let data: NSData |
||||
|
let path: String |
||||
|
|
||||
|
init(baseUrl: String, data: NSData, path: String) { |
||||
|
self.baseUrl = baseUrl |
||||
|
self.data = data |
||||
|
self.path = path |
||||
|
} |
||||
|
|
||||
|
override func main() { |
||||
|
if self.cancelled { |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
var parameters = [ |
||||
|
"file": NetData(data: data, mimeType: MimeType.Json, filename: path), |
||||
|
"name": path |
||||
|
] |
||||
|
|
||||
|
let urlRequest = urlRequestWithComponents(baseUrl + "/service/upload", parameters) |
||||
|
let request = Alamofire.upload(urlRequest) |
||||
|
// .progress { |
||||
|
// (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in |
||||
|
// println("progress : \(totalBytesWritten) / \(totalBytesExpectedToWrite)") |
||||
|
// } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
// |
||||
|
// AlamofireSwiftyJSON.swift |
||||
|
// AlamofireSwiftyJSON |
||||
|
// |
||||
|
// Created by Pinglin Tang on 14-9-22. |
||||
|
// Copyright (c) 2014 SwiftyJSON. All rights reserved. |
||||
|
// |
||||
|
|
||||
|
import Foundation |
||||
|
import Alamofire |
||||
|
import SwiftyJSON |
||||
|
|
||||
|
// MARK: - Request for Swift JSON |
||||
|
|
||||
|
extension Request { |
||||
|
/** |
||||
|
Adds a handler to be called once the request has finished. |
||||
|
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum. |
||||
|
:returns: The request. |
||||
|
*/ |
||||
|
public func responseSwiftyJSON(completionHandler: (NSURLRequest, NSHTTPURLResponse?, SwiftyJSON.JSON, NSError?) -> Void) -> Self { |
||||
|
return responseSwiftyJSON(queue: nil, options: NSJSONReadingOptions.AllowFragments, completionHandler: completionHandler) |
||||
|
} |
||||
|
/** |
||||
|
Adds a handler to be called once the request has finished. |
||||
|
:param: queue The queue on which the completion handler is dispatched. |
||||
|
:param: options The JSON serialization reading options. `.AllowFragments` by default. |
||||
|
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum. |
||||
|
:returns: The request. |
||||
|
*/ |
||||
|
public func responseSwiftyJSON(queue: dispatch_queue_t? = nil, options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest, NSHTTPURLResponse?, JSON, NSError?) -> Void) -> Self { |
||||
|
return response(queue: queue, serializer: Request.JSONResponseSerializer(options: options), completionHandler: { |
||||
|
(request, response, object, error) -> Void in |
||||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { |
||||
|
var responseJSON: JSON |
||||
|
if error != nil || object == nil { |
||||
|
responseJSON = JSON.nullJSON |
||||
|
} else { |
||||
|
responseJSON = SwiftyJSON.JSON(object!) |
||||
|
} |
||||
|
dispatch_async(queue ?? dispatch_get_main_queue(), { |
||||
|
completionHandler(self.request, self.response, responseJSON, error) |
||||
|
}) |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,27 @@ |
|||||
|
// |
||||
|
// Created by Marco Schmickler on 28.05.15. |
||||
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved. |
||||
|
// |
||||
|
|
||||
|
import Foundation |
||||
|
import UIKit |
||||
|
|
||||
|
extension UIImage { |
||||
|
func scaleToSize(width: CGFloat, height: CGFloat) -> UIImage { |
||||
|
UIGraphicsBeginImageContext(CGSizeMake(width, height)); |
||||
|
|
||||
|
let context = UIGraphicsGetCurrentContext(); |
||||
|
CGContextTranslateCTM(context, 0.0, height); |
||||
|
CGContextScaleCTM(context, 1.0, -1.0); |
||||
|
|
||||
|
CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), self.CGImage); |
||||
|
|
||||
|
let scaledImage = UIGraphicsGetImageFromCurrentImageContext(); |
||||
|
|
||||
|
UIGraphicsEndImageContext(); |
||||
|
|
||||
|
return scaledImage; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue