Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Live PhotoをアニメーションGIFに書き出す

Kei Ito
March 30, 2016

Live PhotoをアニメーションGIFに書き出す

関西モバイルアプリ研究会 #12 の資料です
http://kanmoba.connpass.com/event/28411/

Kei Ito

March 30, 2016
Tweet

More Decks by Kei Ito

Other Decks in Technology

Transcript

  1. ඞཁͳ΋ͷ w J04Ҏ্ w 1IPUPT w "7'PVOEBUJPO w *NBHF*0 w

    J1IPOFTT1MVT4&J1BE1SPJODI
  2. -JWF1IPUPΛ୳͢ // get all images let result = PHAsset.fetchAssetsWithMediaType(.Image, options:

    nil) var photoAsset: PHAsset? result.enumerateObjectsUsingBlock { (obj, idx, stop) -> Void in let asset = obj as! PHAsset if asset.mediaSubtypes.contains(.PhotoLive) { photoAsset = asset stop.memory = true } })
  3. -JWF1IPUPΛ୳͢ let mgr = PHImageManager.defaultManager() mgr.requestLivePhotoForAsset(photoAsset, targetSize: CGSizeZero, contentMode: .Default,

    options: nil) { (livePhoto, info) -> Void in if let livePhoto = livePhoto { self.livePhotoView.livePhoto = livePhoto } }
  4. ಈըΛऔΓग़͢ // search movie in live photo let resources =

    PHAssetResource.assetResourcesForLivePhoto(photo) for resource in resources { if resource.type == .PairedVideo { self.getMovieData(resource) break } }
  5. ಈըΛऔΓग़͢ // load movie data let mgr = PHAssetResourceManager.defaultManager() mgr.requestDataForAssetResource(resource,

    options: nil, dataReceivedHandler: { (data) -> Void in movieData.appendData(data) }) { (err) -> Void in do { try movieData.writeToURL(movieURL, options:.AtomicWrite) let movieAsset = AVAsset(URL: movieURL) self.convertToGif(movieAsset, resource: resource) } catch { } }
  6. ը૾ΛऔΓग़͢ // generate thumbnails and write to gif let generator

    = AVAssetImageGenerator(asset: movieAsset) // generate in any timelines generator.requestedTimeToleranceBefore = kCMTimeZero generator.requestedTimeToleranceAfter = kCMTimeZero // apply video transform generator.appliesPreferredTrackTransform = true generator.maximumSize = CGSizeMake(640, 640) generator.generateCGImagesAsynchronouslyForTimes(times) { (requested, image, actual, result, err) -> Void in // }
  7. "OJNBUJPO(*'ʹॻ͖ग़͢ let gif = CGImageDestinationCreateWithURL (gifURL, kUTTypeGIF, numFrame, nil) let

    frameProperty = .. // AVAssetImageGenerator’s loop { CGImageDestinationAddImage(gif, image, frameProperty) } let gifProperty = .. CGImageDestinationSetProperties(gif, gifProperty) CGImageDestinationFinalize(gif) self.webView.loadRequest(NSURLRequest(URL: gifURL))