Copying Gif with UIPasteboard - ios

I want to copy .gif images to clipboard, so as user can paste that gif images from application to mails, etc...
I have tried :
#import MobileCoreServices;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://fc05.deviantart.net/fs37/f/2008/283/a/b/KaleidoCoils_animated__gif_by_1389AD.gif"]];
UIPasteboard *pasteBoard=[UIPasteboard generalPasteboard];
[pasteBoard setData:data forPasteboardType:(__bridge NSString *)kUTTypeGIF];
What happens is the gif is "copied" but my device is no longer to paste. This might be the right code, but there might be a setting on the storyboard that I have to change in order to allow the usage of gifs and UIPasteboard.
Application https://itunes.apple.com/us/app/animated-3d-emoji+emoticons/id429348043?mt=8 already does this.
Please help me.

Related

How can I copy a png image to clipboard in order to paste it in other app?

I'm developing a custom keyboard and I want to be able to copy the button image to clipboard (it's a png image). I've already tried something like UIPasteboard.generalPasteboard().image = UIImage(named: "dMk87zJ.png") when the button is pressed, but it doesn't work. I was only able to accomplish that with text. What am I doing wrong ?
Please use below code for copy and paste images.
NSData *data = [NSData dataWithContentsOfFile:filePath];
[pasteboard setData:data forPasteboardType:#"public.png"];
Its perfect work in all apps which allow paste functionality. Especially Notes app.
For UTI types use this link
Don't use below code
[UIPasteboard generalPasteboard].image = image;
I face issue with Notes app with this line of code to copy png image

How to create custom keyboard extension with images on it in Objective-C?

I am having an app in which I want to create my own custom keyboard.
In this custom keyboard, I want to put images.
Users can access this keyboard from anywhere in the device.
I want it like this Link.
I want to make a keyboard like this
I have searched a lot on this and I know there are lots of tutorials but most of those are in swift and another are not with keyboard extension.
I want proper guidance or any link of tutorial for this.
Any help would be highly appreciated.
Thanks...
So I got my answer from the below link which is a very nice tutorial in objective C.
Custom keyboard with extensions in objective c for ios8
Now, like I needed, if anyone wants to add custom images in it, you can just copy the image to the clipboard and paste the image where its needed.
For copying the png files, use the below code.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *imageName = [NSString stringWithFormat:#"%ld",(long)sender.tag];
NSString *newPathName = [[NSBundle mainBundle]pathForResource:imageName ofType:#"png"];
NSData *data = [NSData dataWithContentsOfFile:newPathName];
[pasteboard setData:data forPasteboardType:#"public.png"];
This public.png comes from http://www.escape.gr/manuals/qdrop/UTI.html
You can choose any extension type you want to.
The pasteboard type changes as per the image extensions.
Important note: For image sending, the Apps which gives an access to paste the image copied from clipboard, only from those apps images will be sent.
I did it with the above solutions. Hope it helps someone else also.
Thanks...
This question is several years old now, but if anyone has the same needs today, I have created a Swift library that helps you create keyboard extensions that support characters, operations like backspace, newline etc. as well as images. It also has support for copying images to the pasteboard and saving them to the photo album.
If you need a library like this, feel free to check it our here:
https://github.com/danielsaidi/KeyboardKit

How to prevent iOS Messages app from cropping pasted image?

I'm working on an app that lets people copy and paste images. The image is copied like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"circle" ofType:#"png"];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [NSData dataWithContentsOfFile:path];
[pasteboard setData:data forPasteboardType:#"public.png"];
When then pasting in the Notes app, it works fine. However in the Messages app, it crops the right side of the image. Is there a way to prevent this?
You cannot prevent this, since that was Apple's UI idea. Although you can put a transparent borders into your image so even though Messages.app clips the image the main part of it still be visible.

How to keep UIWebView from caching images?

I am using UIWebView to display an HTML page inside an IOS app. The HTML page contains a png file that I create on the local disk and write to. Everything works fine the first time I display the page, but when I try to create a new image and redisplay the page, the original image is displayed.
The issue appears to be that I am using the same file name for the PNG file. Even though I write to the PNG file with a new image, the UIWebView is caching the image from the original load, and displays the original image, not the new one. I have verified that the new image is being written to correctly by loading it into Safari.
So how can I clear the UIWebView's cache of this image? I realize that another option would be to give the png file a different file name each time I create it, but then I'd either accumulate png files or I'd have to add code to clear out the png files when done - which I'd rather not do.
Have a look at this answer:
NSString *testURL = [NSString stringWithFormat:#"%#?t=%#", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
You can try:
NSString *theURL = [NSString stringWithFormat:#"%#?t=%#", url, [[NSProcessInfo processInfo] globallyUniqueString]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:theURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:8.0]];

Saving images from web server to phone for image slider

I have an app where it pulls urls of images from a database and stores it in coredata. Then in my app view, there's a scrollview of imageviews, basically I'm trying to do an image slider. These images tend to get updated so it needs to load from the server. I load the image urls from the coredata, download them to uiimages and load them in my view. Problem is, it's really slow as I'm doing it at runtime.
I was wondering what's the best way to go about this. I was thinking of loading all the images to the phone first when the app launches? I tried looking for some sample code to point me to the right direction but I can only find code that saves images to the phone's camera roll and not to the app itself. Anyone could help pls? Thanks!
Here's a method that takes a UIImage, a target path (the directory you want to save the image to) and the filename for the image, and saves the image as a PNG file:
-(BOOL) saveImage:(UIImage *)image savePath:(NSString *)savePath filename:(NSString *)filename{
savePath = [savePath stringByAppendingPathComponent:filename];
NSData *sourceData = UIImagePNGRepresentation(image);
return [sourceData writeToFile:savePath atomically:YES];
}
You will need to store the image in the right location within the app's sandbox. For example, don't put them in the Documents folder as it may be rejected by Apple since you can recreate (or re-download) the images. You likely want to put them in the Library/Caches folder, and be prepared to re-download them if they are removed. See the iOS File System Programming Guide for details on this.
I would be concerned about downloading all the files at app start, since it will look like the app has frozen if it non-responsive while download a number of image files. You can download the images in the background and update the UI as the images finish downloading.
One method to do this would be to check if the image has already been downloaded (e.g., is it in the Library/Caches folder). If it is, then use it, otherwise use a placeholder image until the image is downloaded. For example:
if (isImageAvailable) {
displayedImageView.image = cachedImage;
} else {
displayedImageView.image = placeholderImage;
[self getImageAtURL:imageURL andThenDo:^(UIImage *image) {
// Replace placeholder image with downloaded image
displayedImageView.image = image;
}];
}
Replace isImageAvailable with a method that checks for the existence of the image file in the Library/Caches folder. displayedImageView is the UIImageView you are displaying the image with.
The getImageAtURL method would look something like:
-(void)getImageAtURL:(NSURL *)url andThenDo:(void(^)(UIImage *image))completionBlock{
// Run on background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image;
// Insert code to download image...
// Switch to main (UI) thread to allow updating the UI
dispatch_sync(dispatch_get_main_queue(), ^{
completionBlock(image);
});
});
}
You might not need to download images to your application, I can suggest better you use image url it self to load images from server it self.
To load images from server u may use following:
UIImage* yourImageview = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:your.image_url]]];
Downloading all images will also slow down your app.

Resources