ios - Unable to edit screenshots, performChanges block fails -
i'm developing app allows users edit photos using photokit. saving edited photo disk jpeg. avoid converting jpeg , have implemented modifications in order that. works great photos taken camera, if try edit screenshot, phphotolibrary.sharedphotolibrary().performchanges
block fail , log the operation couldn’t completed. (cocoa error -1.)
. not sure why causing performchanges block fail, have done wrong here?
i've created sample app available download demonstrates problem, , i've included relevant code below. app attempts edit newest photo in photo library. if succeeds prompt access edit photo, otherwise nothing happen , you'll see console log. reproduce issue, take screenshot run app.
current code works screenshots:
let jpegdata: nsdata = outputphoto.jpegrepresentationwithcompressionquality(0.9) let contenteditingoutput = phcontenteditingoutput(contenteditinginput: self.input) var error: nserror? let success = jpegdata.writetourl(contenteditingoutput.renderedcontenturl, options: nsdatawritingoptions.atomicwrite, error: &error) if success { return contenteditingoutput } else { return nil }
replacement code causes screenshots fail:
let url = self.input.fullsizeimageurl let orientation = self.input.fullsizeimageorientation var inputimage = ciimage(contentsofurl: url) inputimage = inputimage.imagebyapplyingorientation(orientation) let outputphoto = createoutputimagefrominputimage(inputimage)! let originalimagedata = nsdata(contentsofurl: self.input.fullsizeimageurl)! let imagesource = cgimagesourcecreatewithdata(originalimagedata, nil) let dataref = cfdatacreatemutable(nil, 0) let destination = cgimagedestinationcreatewithdata(dataref, cgimagesourcegettype(imagesource), 1, nil) //gettype automatically selects jpg, png, etc based on original format struct contextstruct { static var cicontext: cicontext? = nil } if contextstruct.cicontext == nil { let eaglcontext = eaglcontext(api: .opengles2) contextstruct.cicontext = cicontext(eaglcontext: eaglcontext) } let cgimage = contextstruct.cicontext!.createcgimage(outputphoto, fromrect: outputphoto.extent()) cgimagedestinationaddimage(destination, cgimage, nil) if cgimagedestinationfinalize(destination) { let contenteditingoutput = phcontenteditingoutput(contenteditinginput: self.input) var error: nserror? let imagedata: nsdata = dataref let success = imagedata.writetourl(contenteditingoutput.renderedcontenturl, options: .atomicwrite, error: &error) if success { //it succeed return contenteditingoutput } else { return nil } }
the problem happens due fact adjusted photos saved jpg files, , screenshots in fact png files.
it occurred me while debugging sample project , saw in photoeditor, contenteditingoutput.renderedcontenturl
url jpg, while if examine result of cgimagesourcegettype(imagesource)
clear it's png (returns png uti: public.png).
so went , read documentation renderedcontenturl
states if editing photo asset, altered image written in jpeg format - won't work if image png. leads me think apple don't support editing png files or don't want to. go figure..
Comments
Post a Comment