{"action":"created","issue":{"url":"https://api.github.com/repos/onevcat/Kingfisher/issues/465","repository_url":"https://api.github.com/repos/onevcat/Kingfisher","labels_url":"https://api.github.com/repos/onevcat/Kingfisher/issues/465/labels{/name}","comments_url":"https://api.github.com/repos/onevcat/Kingfisher/issues/465/comments","events_url":"https://api.github.com/repos/onevcat/Kingfisher/issues/465/events","html_url":"https://github.com/onevcat/Kingfisher/issues/465","id":181015295,"number":465,"title":"Center crop ImageProcessor","user":{"login":"newmanw","id":3382274,"avatar_url":"https://avatars2.githubusercontent.com/u/3382274?v=3","gravatar_id":"","url":"https://api.github.com/users/newmanw","html_url":"https://github.com/newmanw","followers_url":"https://api.github.com/users/newmanw/followers","following_url":"https://api.github.com/users/newmanw/following{/other_user}","gists_url":"https://api.github.com/users/newmanw/gists{/gist_id}","starred_url":"https://api.github.com/users/newmanw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/newmanw/subscriptions","organizations_url":"https://api.github.com/users/newmanw/orgs","repos_url":"https://api.github.com/users/newmanw/repos","events_url":"https://api.github.com/users/newmanw/events{/privacy}","received_events_url":"https://api.github.com/users/newmanw/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-10-04T21:56:03Z","updated_at":"2017-03-05T21:27:24Z","closed_at":null,"body":"Issue #320 talks about a crop image processor.\n\nDo you think a center crop image processor fits into the current collection of core processors?\n"},"comment":{"url":"https://api.github.com/repos/onevcat/Kingfisher/issues/comments/284264193","html_url":"https://github.com/onevcat/Kingfisher/issues/465#issuecomment-284264193","issue_url":"https://api.github.com/repos/onevcat/Kingfisher/issues/465","id":284264193,"user":{"login":"L4grange","id":8230634,"avatar_url":"https://avatars1.githubusercontent.com/u/8230634?v=3","gravatar_id":"","url":"https://api.github.com/users/L4grange","html_url":"https://github.com/L4grange","followers_url":"https://api.github.com/users/L4grange/followers","following_url":"https://api.github.com/users/L4grange/following{/other_user}","gists_url":"https://api.github.com/users/L4grange/gists{/gist_id}","starred_url":"https://api.github.com/users/L4grange/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/L4grange/subscriptions","organizations_url":"https://api.github.com/users/L4grange/orgs","repos_url":"https://api.github.com/users/L4grange/repos","events_url":"https://api.github.com/users/L4grange/events{/privacy}","received_events_url":"https://api.github.com/users/L4grange/received_events","type":"User","site_admin":false},"created_at":"2017-03-05T21:27:24Z","updated_at":"2017-03-05T21:27:24Z","body":"I put together a quick centre crop processor that I needed for my app, posting it here for anyone that might need it. I have not implemented the anchor yet, if I do I'll create a pull request. :) (P.S.: The code was found on stack overflow, I just put it in an ImageProcessor)\r\n\r\n```swift\r\nimport Foundation\r\nimport Kingfisher\r\n\r\n/// Processor for cropping the center of an image\r\npublic struct CenterCropImageProcessor: ImageProcessor {\r\n\t\tpublic let identifier: String\r\n\t\t\r\n\t\t/// Center point to crop to.\r\n\t\tpublic var centerPoint: CGFloat = 0.0\r\n\t\t\r\n\t\t/// Initialize a `CenterCropImageProcessor`\r\n\t\t///\r\n\t\t/// - parameter centerPoint: The center point to crop to.\r\n\t\t///\r\n\t\t/// - returns: An initialized `CenterCropImageProcessor`.\r\n\t\tpublic init(centerPoint: CGFloat? = nil) {\r\n\t\t\tif let center = centerPoint {\r\n\t\t\t\tself.centerPoint = center\r\n\t\t\t}\r\n\t\t\tself.identifier = \"com.l4grange.CenterCropImageProcessor(\\(centerPoint))\"\r\n\t\t}\r\n\t\t\r\n\t\tpublic func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {\r\n\t\t\tswitch item {\r\n\t\t\tcase .image(let image):\r\n\t\t\t\t\r\n\t\t\t\tvar imageHeight = image.size.height\r\n\t\t\t\tvar imageWidth = image.size.width\r\n\t\t\t\t\t\t\t\r\n\t\t\t\tif imageHeight > imageWidth {\r\n\t\t\t\t\timageHeight = imageWidth\r\n\t\t\t\t}\r\n\t\t\t\telse {\r\n\t\t\t\t\timageWidth = imageHeight\r\n\t\t\t\t}\r\n\t\t\t\t\t\t\t\r\n\t\t\t\tlet size = CGSize(width: imageWidth, height: imageHeight)\r\n\t\t\t\t\t\t\t\r\n\t\t\t\tlet refWidth : CGFloat = CGFloat(image.cgImage!.width)\r\n\t\t\t\tlet refHeight : CGFloat = CGFloat(image.cgImage!.height)\r\n\t\t\t\t\t\t\t\r\n\t\t\t\tlet x = (refWidth - size.width) / 2\r\n\t\t\t\tlet y = (refHeight - size.height) / 2\r\n\t\t\t\t\t\t\t\r\n\t\t\t\tlet cropRect = CGRect(x: x, y: y, width: size.height, height: size.width)\r\n\t\t\t\tif let imageRef = image.cgImage!.cropping(to: cropRect) {\r\n\t\t\t\t\treturn UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\treturn nil\r\n\t\t\t\t\r\n\t\t\tcase .data(_):\r\n\t\t\t\treturn (DefaultImageProcessor.default >> self).process(item: item, options: options)\r\n\t\t\t}\r\n\t\t}\r\n}\r\n\r\n```"}}
{"actor":{"display_login":"L4grange"}}