CALayer renderInContext iOS7 - ios6

I'm rendering multiple layers to get one final image. One of the images contains a face, and another one contains a background with a transparent gradient around the face so that the real background is hidden. In iOS6 it works perfectly but it is creating a weird effect with the transparent gradient in iOS7.
The code:
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[faceImageView.layer renderInContext:context];
[fakeBackgroundImageView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The results in iOS6 and iOS7:

Ok, the solution was changing this line:
UIGraphicsBeginImageContext(rect.size);
for this one:
UIGraphicsBeginImageContextWithOptions(rect.size, TRUE, [[UIScreen mainScreen] scale]);
Now it also works in iOS7

Related

How to fix VisualBrush lost line?

In WPF ,we can use VisualBrush do some thing like ppt's left side.
But I see the VisualBrush may lost the line in Rectangle when I zoom the VisualBrush to a small size.Like the image:
You can see VisualBrush lost the Bottom line.
But what I want is like the below image:
When I try use the BitmapImage that use RenderTargetBitmap to get a image and use linear interpolation algorithm to zoom will get a clearness image.
Can I change VisualBrush's algorithm I think it may use neighborhood-pixels algorithm.
Are there any printscreen algorithm that have a good performance like VisualBrush.
When I change my search key to ViewBox ,I can find the same question as this one :how to avoid a single pixel line disappear in wpf?
There is a class named TransformedBitmap which can scale your RenderTargetBitmap with default scaling algorithm.
Use the code below:
public static BitmapSource ToBitmapSource(this Visual visual, Size size)
{
var bounds = VisualTreeHelper.GetDescendantBounds(visual);
var width = (int) Math.Round(bounds.Width);
var height = (int) Math.Round(bounds.Height);
var bitmap = new RenderTargetBitmap(width, height, 96.0, 96.0, PixelFormats.Pbgra32);
bitmap.Render(visual);
return new TransformedBitmap(bitmap, new ScaleTransform(size.Width / width, size.Height / height));
}
I've tried this method in my demo and got the result below. You may noticed that the small rectangle in the left-top corner lost nothing.

Rotation issue from the first view (splash) to first viewController

I am working in Xcode 4.5.2, targeting iOS6 for an iPad app, using storyboards and segues.
Preamble: my root controller (loaded by the app delegate) is a splash screen with only an image, an upgrade button and an open button. App takes a few seconds to load. I have shouldAutorotate and supportedInterfaceOrientations in all three of my full screen controllers. For rotation notification, I am using the following two methods in my root view controller:
- (void)awakeFromNib
{
[UIDevice.currentDevice beginGeneratingDeviceOrientationNotifications];
[NSNotificationCenter.defaultCenter addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Landscape
CGRect rect = _libraryViewController.libraryTableBorder.frame;
_libraryViewController.libraryTableBorder.frame = rect;
_libraryViewController.libraryTableBorder.image = [UIImage imageNamed:#"LibraryBorder_L.png"];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
// Portrait
CGRect rect = _libraryViewController.libraryTableBorder.frame;
_libraryViewController.libraryTableBorder.frame = rect;
_libraryViewController.libraryTableBorder.image = [UIImage imageNamed:#"LibraryBorder_P.png"];
}
}
I have these very same methods in the LibraryViewController and it works perfectly. I have another main view controller (entryView) that has the same methods without the calls for the libraryTableBorder. No matter what rotation the device is in coming from or going to the entry view, the table border swaps out correctly. And, when going from the library to either the entryView or to the splash, the views are correct.
The issue is going from the splash view in landscape to the library. Going to the library in Potrait works fine and the border displayed is the portrait border. But, in landscape, it also displays the portrait border. How can I get the library border to display in landscape when coming from the root view when it is in landscape?
Any help in solving this conundrum would be much appreciated!!!
I have found the solution to this issue... viewWillLayoutSubviews
I was prompted by another thread, which pointed to the release notes for iOS6. I added the UIDeviceOrientation and my if/else statement to it. Now the border rotates correctly, regardless of which view I go to or come from!
- (void)viewWillLayoutSubviews
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
// Landscape
CGRect rect = _libraryTableBorder.frame;
_libraryTableBorder.frame = rect;
_libraryTableBorder.image = [UIImage imageNamed:#"LibraryBorder_L.png"];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
// Portrait
CGRect rect = _libraryTableBorder.frame;
_libraryTableBorder.frame = rect;
_libraryTableBorder.image = [UIImage imageNamed:#"LibraryBorder_P.png"];
}
}
This was sure a frustrating issues for me. Hope this helps someone else!

Is it possible to change an ImageBrush color inside WPF?

I'm trying to do something quite simple, but I'm not sure if it's possible. I have this image:
Inside my board game project, I use this to indicate the mouse position in the board and stuff like that (being the board a 8x8 grid with a rectangle inside). I'm also setting the grid cell background brush as, say, a SolidColorBrush with Blue color and .25 opacity, while the rectangle that lies above it with the target, or vice-versa, because I couldn't achieve my desired effect, which is, the part that is black to turn full blue, as my brush, while the rest remains with the opacity and etc. The way I did, it was possible to make the black part become blueish, with a transluscent background, but the color in the border isn't quite right yet:
I don't know if I was able to make myself clear, but I guess the picture shows what is my goal.
The code:
ImageBrush targetBrush = new ImageBrush();
targetBrush.ImageSource = new BitmapImage(new Uri("Resources/GameScreen/selectedTile.png", UriKind.Relative));
targetBrush.Opacity = 1;
SolidColorBrush backBrush = new SolidColorBrush(Colors.Blue);
backBrush.Opacity = .25;
tile.Children.Add(new Rectangle());
foreach (object target in tile.Children)
{
if (target.GetType() == typeof(Rectangle))
{
((Rectangle)target).Fill = backBrush;
}
}
tile.Background = targetBrush;
Thanks in advance.
As the image is just a mask you could use a Rectangle with a blue Fill and an OpacityMask set to your image.

Dark vertical line on a panorama control background image edge in Windows Phone

I set a panorama backgroundBrush the following way:
In DataContext:
var bitmapImage = new BitmapImage(new Uri("../Images/panorama.background.png", UriKind.RelativeOrAbsolute));
var backBrush = new ImageBrush { ImageSource = bitmapImage };
PanoramaBackgroundBrush = backBrush;
In View:
<controls:Panorama Background="{Binding PanoramaBackgroundBrush}"
The panorama.background.png is a white image with an app logo on top.
Whenever I scroll over the edge of Panorama background (from last panorama item to the first one), a vertical thin dark line appears at a time of motion. When the transition stops, the line disappears.
You can see the problem on a video of a simulator I have uploaded to youtube.
It is almost as if the rendering engine of WP SL is not catching up to redraw the transition of the image's edge. But the same background is in the Office Hub, and it doesn't have a problem I am experiencing.
Please advise on how to solve the vertical line problem.
I can reproduce this and sometimes even got bigger gaps while testing.
I think the office hub suffers from the same problem but is faking it away by using a white background. And you can do so, too:
<Grid x:Name="LayoutRoot" Background="White">
This gives the grid behind the Panorama a white background. If your image is white at the edge (like in the YouTube video) then the glitch won't be visible anymore.

WPF Background setting failed

Here is a code.
var image = new BitmapImage(new Uri(#"pack://application:,,,/Images/background.png",
UriKind.RelativeOrAbsolute));
var backgroundBrush = new ImageBrush()
{
ImageSource = image,
Viewport = new Rect(0, 0, image.PixelWidth / ActualWidth,
image.PixelHeight / ActualHeight),
TileMode = TileMode.Tile,
Stretch = Stretch.None,
};
// Set it for the main window.
Background = backgroundBrush;
It works just fine on my PC with XPSP3 and .Net 4.0. But when I run the same sample on Eee PC T91MT with Windows 7 Home Premium it fails. No exceptions, but nothing is drawn (solid color brushes ARE drawn if used instead, though). I thought it could be the result of limited resources, but on Viliv S5, that has about the same specs it works fine too.
Any ideas?
Thanks!
UPDATE
The root of the problem is Viewport's rect. Since the bitmap has twice window's size by X, the rect is (0, 0, 2, 1). So, on power computer with XPSP3, the left half of the image is drawn. But on Eee PC it causes a problem with visualization.
The answer is just normalizing Viewport rectangle. E.g. instead of (0,0,2,1) I had to set it as (0,0,1,0.5).
I'm not sure, but it looks like WPF just transmit rect values (after some transformation) into a D3D driver, which is (or is not) able to handle it the right way. So, non-normalized rect Viewport works on GeForce-based machine but does not on Eee PC with it's integrated video's driver.

Resources