is there any way to modify/edit phasset image by cropping or edited one?
I have array of asset, I wanna crop image of asset by getting image from selected asset and passing it cropping controller and in return want to change that cropped image in selected asset.
There is my code to understand well
func presentCropViewController(with:IndexPath) {
self.allPhotos[with.item].getImage { (img) in
if let image = img{
self.indexPathForCropping = with
let cropViewController = CropViewController(image: image)
cropViewController.delegate = self
cropViewController.view.tintColor = UIColor.themeGreen()
self.present(cropViewController, animated: true, completion: nil)
}
}
}
after passing image from asset I got cropped image with this method
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
cropViewController.dismiss(animated: true, completion: nil)
// Here i get the cropped image and want to update selected asset with this image
}
I'll appreciate if you mentioned down vote reason too so I prepare my question accordinglly
I figured out the solution may its not an efficient way to do but solved my problem
extension PHAsset {
func updateChanges(with img:UIImage,completion:#escaping(PHAsset?)->()){
PHPhotoLibrary.shared().performChanges({
// create cropped image into phphotolibrary
PHAssetChangeRequest.creationRequestForAsset(from: img)
}) { (success, error) in
if success{
// fetch request to get last created asset
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
fetchOptions.fetchLimit = 1
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
if let asset = fetchResult.firstObject{
// replace your selected asset with new cropped one
completion(asset)
}else{
completion(nil)
}
}else{
completion(nil)
}
}
}
}
simply pass cropped/modified image and get new asset with same cropped/modified image
Related
I'm trying to load a set of pictures to an imageView so that they slide alon with a time timer.
My problem is that I have the pictures store in Firebase with their URL as Strings. So I use a function to load a single image throug its URL into an imageView and it works. But now I need to find a way to use a function to put a set of images into the imageView so that the slide with a timer and I donĀ“t now how to do it.
This is the code to load the images from URL
import UIKit
extension UIImageView {
func loadFrom(URLAddress: String) {
guard let url = URL(string: URLAddress) else {
return
}
DispatchQueue.main.async { [weak self] in
if let imageData = try? Data(contentsOf: url) {
if let loadedImage = UIImage(data: imageData) {
self?.image = loadedImage
}
}
}
}
}
Here is the code to load the set of image as an UIImage array.
override func viewWillAppear(_ animated: Bool) {
let even = eventos.eventoPulsado()
var images = [UIImage]()
print(even.imagenes!)
//I load the picture this way
imgFoto.loadFrom(URLAddress: (even.imagenes![0]))
//But I cannot to append my images because I need UIImage data
images.append(imgFoto.image!)
for img in (even.imagenes!) {
imgFoto.loadFrom(URLAddress: (img))
}
imgFoto.animationImages = images
imgFoto.animationDuration = TimeInterval(0.4)
imgFoto.animationRepeatCount = 4
imgFoto.startAnimating()
}
I need to load an ImageView inside UIcollectionViewcell using a URL that I pass during initialisation:
func configureCellWith(messageModel : MessageModel){
guard let url = URL(string: messageModel.contentUrl!) else { return }
if url.isURLPhoto(){
likedImageView.sd_setImage(with: url, placeholderImage: nil)
}
else if url.isURLVideo(){
getThumbnailImageFromVideoUrl(url: url) { (image) in
self.likedImageView.image = image
}
}
If url is video I need to load the image in this way using this method:
func getThumbnailImageFromVideoUrl(url: URL, completion: #escaping ((_ image: UIImage?)->Void)) {
DispatchQueue.global().async {
let asset = AVAsset(url: url)
let avAssetImageGenerator = AVAssetImageGenerator(asset: asset)
avAssetImageGenerator.appliesPreferredTrackTransform = true
let thumnailTime = CMTimeMake(value: 2, timescale: 1)
do {
let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumnailTime, actualTime: nil)
let thumbNailImage = UIImage(cgImage: cgThumbImage)
DispatchQueue.main.async {
completion(thumbNailImage)
}
} catch {
print(error.localizedDescription)
DispatchQueue.main.async {
completion(nil)
}
}
}
}
As visible I retrieve the initial frame of the video and I load it inside the cell, obviously since it's an asynchronous function it will take some time for loading the image, there's no problem In that.
The problem occurs when I scroll through the collection and I see that some cells display images which don't correspond to the correct ones.
Searching online I found out that I need to clear the image in prepareForReuse of the cell and so I did (both in case the image is loaded through sd_setImage and though getThumbnailImageFromVideoUrl function):
override func prepareForReuse() {
super.prepareForReuse()
self.likedImageView.image = UIImage()
self.likedImageView.image = nil
self.likedImageView.sd_cancelCurrentImageLoad()
}
but I still get images mismatched when scrolling thought the collection view, what could be the problem?
I think the issue is not with images, i guess its with video thumbnail. You generate a thumbnail on background thread synchronously but while setting it back to imageView you never bothered to find if the cell is reused and the image u just created is outdated or not.
So in your cell
var currentModel: MessageModel! = nil //declare a instance variable to hold model
... other code
func configureCellWith(messageModel : MessageModel){
self.currentModel = messageModel //keep a copy of model passed to u as argument
guard let url = URL(string: messageModel.contentUrl!) else { return }
if url.isURLPhoto(){
likedImageView.sd_setImage(with: url, placeholderImage: nil)
}
else if url.isURLVideo(){
getThumbnailImageFromVideoUrl(url: url) { (image) in
self.likedImageView.image = image
}
}
Finally in getThumbnailImageFromVideoUrl
func getThumbnailImageFromVideoUrl(url: URL, completion: #escaping ((_ image: UIImage?)->Void)) {
DispatchQueue.global().async {
let asset = AVAsset(url: url)
let avAssetImageGenerator = AVAssetImageGenerator(asset: asset)
avAssetImageGenerator.appliesPreferredTrackTransform = true
let thumnailTime = CMTimeMake(value: 2, timescale: 1)
do {
let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumnailTime, actualTime: nil)
let thumbNailImage = UIImage(cgImage: cgThumbImage)
if url.absoluteString == currentModel.contentUrl { //check if image you generated is still valid or its no longer needed
DispatchQueue.main.async {
completion(thumbNailImage)
}
}
} catch {
print(error.localizedDescription)
DispatchQueue.main.async {
completion(nil)
}
}
}
I have UIImageView which is displaying GIF image, now I am unable to save GIF image into photo gallery.
I am using below swift code
guard let image = imageView.image else { return }
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(savedImage),
nil);
When I use above code it only save single image in photo gallery not saving as GIF.
Suggestion required, how to save GIF loaded from URL or available in the UIImageView into Gallery.
First your need to get all images of gif from imageView and the duration of the image, now you can generate the gif and save in document directory. Finally, save the file from URL to Photo Album
import ImageIO
import Photos
import MobileCoreServices
#IBAction func btnSaveGifFilePressed(_ sender: Any) {
if let imgs = self.imgView.image?.images, imgs.count > 0 {
var animationDuration = self.imgView.image?.duration ?? 1.0
animationDuration = animationDuration / Double(imgs.count);
let url = FileManager.default.urlForFile("lion.gif")
FileManager.default.createGIF(with: imgs, url: url, frameDelay: animationDuration)
PHPhotoLibrary.shared().performChanges ({
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: url)
}) { saved, error in
if saved {
print("Your image was successfully saved")
}
}
}
}
Create GIF using the following code.
func createGIF(with images: [UIImage], url: URL, frameDelay: Double) {
if let destinationGIF = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, images.count, nil) {
let properties = [
(kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): frameDelay]
]
for img in images {
let cgImage = img.cgImage
CGImageDestinationAddImage(destinationGIF, cgImage!, properties as CFDictionary?)
}
CGImageDestinationFinalize(destinationGIF)
}
}
Get a URL in single line:
func urlForFile(_ fileName: String, folderName: String? = nil) -> URL {
var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
if let folder = folderName {
documentsDirectory = documentsDirectory.appendingPathComponent(folder)
if !self.fileExists(atPath: documentsDirectory.path) {
try? self.createDirectory(atPath: documentsDirectory.path, withIntermediateDirectories: true, attributes: nil)
}
}
let fileURL = documentsDirectory.appendingPathComponent(fileName)
return fileURL;
}
In my app, I take a photo from my camera, and then the user can add a sticker gif on it.
I want to save the result on Firebase Storage (in a gif format)
So to summarize, I want to convert my UIview into Data, to save it on Firebase ( Storage.storage().reference().child("Gif").putData(data, metadata: nil, completion: { (metadata, error) in })
I have tried that :
let yourViewToData = NSKeyedArchiver.archivedData(withRootObject: self.canvasView)
but the result isn't a gif file
I have tried a func like this but I cannot intercept Data on it and I'm not sure of the result :
let img = self.canvasView.toImage()
animatedGif(from: [img])
func animatedGif(from images: [UIImage]) {
let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary
let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): 1.0]] as CFDictionary
let documentsDirectoryURL: URL? = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL: URL? = documentsDirectoryURL?.appendingPathComponent("animated.gif")
if let url = fileURL as CFURL? {
if let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, nil) {
CGImageDestinationSetProperties(destination, fileProperties)
for image in images {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, frameProperties)
}
}
if !CGImageDestinationFinalize(destination) {
print("Failed to finalize the image destination")
}
print("Url = \(fileURL)")
}
}
}
Any idea how I can save my UIView to a gif file on Firebase Storage?
I am looking to make an alternative solution . Take like 15 screenshot of UImage from my UIView, in order to make a gif later. I have this func :
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
self.drawHierarchy(in: self.bounds, afterScreenUpdates: false)
let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshotImageFromMyView!
}
Does someone know how can I make like 15 screenshot with this func? Thanks
Thanks !
I have a bunch of annotations on a map, which all have a custom photos. Some of the photos may not be downloaded to the application yet from Firebase, so if they do not the image available, it defaults to a white circle image and initiates a download. When the download completes, it does not set the new image. How can I go about this?
Heres some code:
func generateAnnotations() {
for photo in photos {
let annotation = DetailPhotoPointAnnotation()
let latitude = photo.latitude
let longitude = photo.longitude
annotation.coordinate.latitude = latitude
annotation.coordinate.longitude = longitude
if photo.image != nil {
annotation.image = photo.image
} else {
annotation.image = #imageLiteral(resourceName: "whiteCircle")
let path = getDocumentsDirectory().appendingPathComponent(photo.uid)
if let image = UIImage(contentsOfFile: path) {
annotation.image = image
} else {
let ref = FIRStorage.storage().reference(forURL: photo.imageUrl)
ref.data(withMaxSize: 5*1024*1024, completion: { (data, error) in
if error != nil {
print(error!)
} else {
if let imageData = data {
if let image = UIImage(data: imageData) {
photo.assignImage(image: image)
annotation.image = image
}
}
}
})
}
}
self.coordinates.append(CLLocationCoordinate2DMake(latitude, longitude))
self.mapView.addAnnotation(annotation)
}
generateOverlay()
}
As you can see, it first looks to if the photo object contains an image. If it doesn't, it looks in the documents directory for that image. If its not there, it will finally download it. Any suggestions?
You need to do something like this. Go to main thread. And then update
DispatchQueue.main.async(execute: {
imageView.image = image
})
In my solution it works.
Hope this helps.