iOS 7 - How to eliminate white area on top of screenshot - ios7

Using Xcode 5.1 and target 7.1 or 6.1 I am capturing a screenshot and posting it for an e-mail. The image in the e-mail has an unwanted white area on top. I am using the following code:
CGSize mySize;
mySize.width = 320;
mySize.height = 480;
UIGraphicsBeginImageContextWithOptions( mySize, YES, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *newPNG=UIImagePNGRepresentation(img);
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:[NSArray arrayWithObjects:newPNG, nil] applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeCopyToPasteboard, UIActivityTypePrint];
[self presentViewController:activityVC animated:YES completion:nil];
I would like to eliminate the excessive white space on top of the obtained screenshot. Thanks.

Related

How to make a screen cut of a scroll window with the whole part not only the visible part on mac

I am writing a sreen cut APP on mac OS X, i can only get the visible part of window like this:
CGImageRef imageRef = CGWindowListCreateImage(CGRectNull,
kCGWindowListOptionIncludingWindow,
self.windowID,
kCGWindowImageBoundsIgnoreFraming);
_windowImage = [[NSImage alloc] initWithCGImage:imageRef size:self.windowFrame.size];
CGImageRelease(imageRef);
But when the window can scroll, i want to get the whole part not only the visible part, does anyone have some advices?
Thanks!
//capture 获取scrollview的截图
NSRect bounds = NSMakeRect(0, 0, [self.scrollView.documentView frame].size.width,[self.scrollView.documentView frame].size.height);
// //只能获取可见部分 only can grap the visible part of the scrollview
// NSBitmapImageRep* captureImage = [(NSView *)self.scrollView.documentView bitmapImageRepForCachingDisplayInRect:bounds];
// [self.scrollView cacheDisplayInRect:bounds toBitmapImageRep:captureImage];
// NSImage *image = [[NSImage alloc] initWithCGImage:[captureImage CGImage] size:captureImage.size];
//can grab the whole part
[self.scrollView lockFocus];
NSImage *image = [[NSImage alloc] initWithData:[(NSView *)self.scrollView.documentView
dataWithPDFInsideRect:bounds]];
[self.scrollView unlockFocus];

How to create image from screenshot and send to message interface

I am very new to Objective-C. I am attempting to create an image from a screen shot and send it to the message interface to be sent as a message. I have looked at numerous Stackoverflow questions and one suggests use of snapshotViewAfterScreenUpdates which is new in iOS 7. It returns a UIView pointer but I do not understand how to make an image from that and then to the message interface to be sent as a message. Any help would be greatly appreciated.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *newPNG=UIImagePNGRepresentation(img); // or you can use JPG or PDF
To share the image, then you can use UIActivityViewController:
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:#"I would like to share it.",newPNG, nil] applicationActivities:nil];
activityVC.excludedActivityTypes = #[ UIActivityTypeMessage ,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:nil];
This is you you get UIImage from screen:
UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

not able to see selected button image in iOS 7

I have app where I am using tab bar controller. For tab bar buttons, I have images of size 30x30 and 60x60.
When I run my app in iOS 6, it shows me selected image, however when I switch to iOS 7, it don't show me selected tab image. Any idea why this is happening?
Below are screenshots for the same.
iOS 6
iOS 7
Edit 1
On further investigation, I found that this is happening only with first two tab bar. From third to fifth, images are showing fine for iOS7.
Code used is
UIImage *selectedImage0 = [UIImage imageNamed:#"home_selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"home.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"search_selected.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"search.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"ad_with_us_selected.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"ad_with_us.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"aboutus_selected.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"aboutus.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"contacts_selected.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"contacts.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[[UITabBar appearance] setSelectionIndicatorImage:[[UIImage alloc] init]];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
Edit 2
Also below is not working in iOS 7.
[tabItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:localize(#"fontNameN") size:10.0f], NSFontAttributeName, [UIColor greenColor], NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
and for forState:UIControlStateHighlighted also
Note
I had created this project in Xcode 4.2 and now for iOS 7, I am opening in Xcode 5. Will that be a problem?
Just use
[item0 setSelectedImage:selectedImage0];
and so on, because
setFinishedSelectedImage:withFinishedUnselectedImage:
is deprecated in iOS 7
Believe it or not
When I remove text that is default in Title and Image from IB, all is working perfectly now.
Apple only knows what was problem

How to add image from url to MKAnnotationView for retina display?

I'm trying to create a custom MKAnnotationView with image from a url.
What happens is that when the device has retina display, the image of the MKAnnotationView is blurred, as it double its resolution.
If the image is from the app, it will load the #2x image (if one exists), but if you set an image from a url like this for example:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation
{
MKAnnotationView *customAnnotationView=[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://www.interaction-design.org/images/icons/play-button-red-300x300.png"]];
UIImage *img = [UIImage imageWithData:imageData];
[customAnnotationView setImage:img ];
return customAnnotationView;
}
you will see the image on an retina display very pixelated.
Any advice what to do?
Thanks
The gist is this...
Use [[UIScreen mainScreen] scale] to ask your server for a standard or #2x image.
Create a CGImageRef from the NSData
Use the CGImageRef and [[UIScreen mainScreen] scale] to create a UIImage
The code to do that looks like this:
NSString *imagePath = #"http://www.interaction-design.org/images/icons/play-button-red-300x300";
imagePath = [imagePath stringByAppendingString:[[UIScreen mainScreen] scale] > 1 ? #"#2x.png": #".png"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imagePath]];
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)imageData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGDataProviderRelease(dataProvider);
CGImageRelease(imageRef);
Just note that you will need to have two resolutions of the image on the server given the first two lines of my code. Currently the image will appear smaller on retina, larger on non retina screens.

How to set an image on tab bar controllers?

I am trying to change the image of a Tab Bar in my application. When I changed the image it was giving me a blank image instead.
Can I set .png format image?
Is it possible to change the color of the tab bar?
Can I use a colored image in the tab bar?
No, You can use .icon images for tabbar which has transparent background and black and white. It is possible to set color of tabbar also.
tabbar Color
CGRect frame = CGRectMake(0.0, 0, 320, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.4 alpha:0.3]];
//[v setAlpha:1.0];
[[tabbar tabBar] insertSubview:v atIndex:0];
[v release];
in iOS5 all of those things are possible!
UIImage *selectedImage0 = [UIImage imageNamed:#"TabBa1selected.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"TabBa2selected.png.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"TabBa3selected.png.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:selectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:selectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:selectedImage2];
UIImage* tabBarBackground = [UIImage imageNamed:#"tabBarBackground.png"];
UIImage* tabBarSelected = [UIImage imageNamed:#"SelectedImage.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelected];
You would typically set an image for your tab bar in the AppDelegate class. It likely has a tab bar controller defined as _tabBarController, so the code would be:
[[_tabBarController tabBar] setBackgroundImage:[UIImage imageNamed:#"imageName.png"]];
If the image provided is of the proper size or stretchable it will be that image, otherwise it will be the image tiled however many times it takes to fill the tab bar.
You could also change the tab color if you'd like.