UIPasteBoard Changes my Image Orientation - iphone

I am Copying the array of Images in UIPasteBoard and When I try to Paste the images in mail the Image orientation is changing. This is How I am Copying the Images.
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
pasteBoard.images = imageArray;
How can I copy the images without changing the Image orientation ?

UIImage's imageWithCGImage:scale:orientation: method can modify the orientation if you're not pleased with the results.
What your mail app is displaying is probably the true pixel array before the orientation info is applied to rotate it.
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageWithCGImage:scale:orientation:

I have this working. I just use the setData to give it raw data and then set the data type using forPasteboardType. Right below your
if (fileExists){
Try this
NSData *data = [NSData dataWithContentsOfFile:imagefile];
[pasteboard setData:data forPasteboardType:#"public.png"];
May this Helping to you.

Related

UIImagePNGRepresentation save a portrait image in landscape orientation

After I get an image from iPhone gallery using imagePickerController I want to save that image in the app document directory, so I use this code:
-(void)saveImage:(UIImage*)image{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/background.png"];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
but the resulting file is in Landscape orientation also if the image was in Portrait orientation...
If I try to see the image in a imageView on the xib the image result correct... (for example a 2448 × 3264 pic is showed right) but after I have saved that image the file that has been created is 3264 × 2448...
Anyone know what can I do to avoid this change?
Thanks a lot. Massy
I finally understood why this was happening and find a solution... solution is to save Jpeg and not PNG as PNG lose the orientation when saving.

How to use UIactivityindicatorview while downloading an image from a url

In my app I have implemented a method to download a image from a URL and display it in a TableView cell. The code is working fine but I also need to implement two more functions,
1) a UIActivityIndicator to show that the image is being downloaded.
2) Check whether the image is already downloaded. Download only if the cell is empty.
Here is my code.
-(void) downloadImage
{
NSURL *url = [NSURL URLWithString:#"http://cdn.iphonehacks.com/wp-content/uploads/2012/09/iphone5-front-back.jpg"];
UIImage *image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL:url]];
[dCellImageView setImage:image];
[image release];
}
Thanks.
Use [NSData dataWithContentsOfURL:URL options:NSDataReadingMapped error:&error]. It is a synchronized method. So probably start activityIndicator before sending the message, and check error, then stop activityIndicator. What do you mean by 'cell is empty'? Do you store image in disk or just memory. If former, use NSFileManager to check if file exists.
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
It'll show an indicator on the left of the status bar. You can also use MBProgressHUD if you like.
And if you want to check whether the image has downloaded already before downloading, you need to store a key in your Core Data or Property List.

UIImage display differently when saving and loading from file

I'm download jpeg picture from web server and load it to UIImage.
If I display the UIImage in UIImageView directly, I see the picture correctly.
But if i cache the image to file with :
[UIImageJPEGRepresentation(image,1.0f) writeToFile:sFilePath atoimcally:YES]
and load it with :
image=[UIImage imageWithContentsOFile:sFilePath]
and display this in the same UIImageView, I can see white stripes in the sides of the picture.
again, Exactly the same UIImageView object with the same properties settings in it.
Why is that?
You can simply write to a file the NSData you have loaded from the web, without going through the UIImageJPEGRepresentation routine.
[dataObject writeToURL:fileURL atomically:YES];
and to retrieve
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[fileURL path]];
That works really well in my app.

How to save a png image to camera roll?

I'd like to save screenshots from my game to camera roll.
Right now I'm using UIImageWriteToSavedPhotosAlbum which saves the image in jpg format with way too much ugly jpg artifacts.
Is there a way to save an UIImage in png format to camera roll?
First you have to add AssetsLibrary Framework, than like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image = ... some image
[library writeImageDataToSavedPhotosAlbum: UIImagePNGRepresentation(image) metadata:nil completionBlock:nil];
The solution pointed out here is now deprecated - this post:
UIImageWriteToSavedPhotosAlbum saves to wrong size and quality
shows how to do this:
UIImage* im = [UIImage imageWithCGImage:myCGRef]; // make image from CGRef
NSData* imdata = UIImagePNGRepresentation ( im ); // get PNG representation
UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); // save to photo album
Napolit "I'd like to save screenshots from my game to camera roll. Right now I'm using UIImageWriteToSavedPhotosAlbum which saves the image in jpg format with way too much...."
Have just stumbled across your question, most of which I can't answer. however, I do have an EASY & QUICK WAY to grab iPad screens. Simply press the SLEEP/WAKE button and the HOME (round button at bottom of the screen) button at the same time.
This tells how to represent a UIImage as a jpg or png: Save UIImage Object as PNG or JPEG File

How to save a stretched image?

How can I save a stretched image?
I have a small(50 X 50) image, that I have stretched while displaying it on a UIImageView (by choosing scaletofit mode.) Now I need to fetch & save that stretched image.
I have tried by using UIImageView's image property, but it gives me original image not that stretched image.
So, do you have any idea how to solve this problem? If then please help me or guide me or at-least give me a hint.
Hoping for your reply.........
Thanks
You have to create an image by copying the bit map of the view's CGContext (core graphic context). This has been asked on stackoverflow before but I can't find the answer right now.
You need to render your UIImageView into a graphic context you create :
UIGraphicsBeginImageContext(yourUIImageView.frame.size);
yourUIImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//get the PNG representation of the UIImage (but you could do the same with JPEG
NSData *myData = UIImagePNGRepresentation(screenshot);
//then save it to a file
[myData writeToFile:thePathToWriteYourFileAsNSString atomically:YES];