Binding Image URIs via Image.SetBinding() not working properly - forms

I'm using the following code snippet to bind a image url to a image object (used as one element in a ViewCell object).
...
Image Picture = new Image()
{
Aspect = Aspect.AspectFit,
HorizontalOptions = LayoutOptions.StartAndExpand
}; // ImageSource.FromUri(new Uri("sImageUrl")))
Picture.SetBinding(Image.SourceProperty, "sImageUrl");
...
The images in the list, for which I'm using that cell view, are not loading always. I was not able to identify the exact reason for the problem, but I think the problem is the load-process (for loading the images from the url/internet)..
Maybe the problem could be solved by setting the url via new Uri(...) as described in the documentation
var webImage = new Image { Aspect = Aspect.AspectFit };
webImage.Source = ImageSource.FromUri(new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"));
Now my question: is there a workaround for binding a uri object? e.g.
...
Image Picture = new Image()
{
Aspect = Aspect.AspectFit,
HorizontalOptions = LayoutOptions.StartAndExpand
}; // ImageSource.FromUri(new Uri("sImageUrl")))
Picture.SetBinding(Image.SourceProperty, ImageSource.FromUri(new Uri("sImageUrl")));
...
I'm working with xamarin studio (version 6.1.2, build 44, update channel "beta", os x).
Would be great if someone got a tipp.
Thanks a lot,
Alex

after some more trying I found a solution.. I'm not sure if it's a secure / professional way.. But for now, it seems to work.
What I did: I implemented my own class "AsyncSrcImage", derived it from Image and added another bindable property:
public class AsyncSrcImage : Image
{
private static String sDefaultURL = "";
public static readonly BindableProperty AsyncImgUrlProperty = BindableProperty.Create(nameof(AsyncImgUrl), typeof(String), typeof(AsyncSrcImage), sDefaultURL);
public String AsyncImgUrl
{
get { return (String)GetValue(AsyncImgUrlProperty); }
set { SetValue(AsyncImgUrlProperty, value); }
}
}
Additionally I adjusted the rendering process with a custom renderer. There I'm loading the image source via new Uri(...) as described in the documentation:
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using System;
[assembly: ExportRenderer(typeof(AsyncSrcImage), typeof(AsyncSrcImageRenderer))]
public class AsyncSrcImageRenderer : ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
AsyncSrcImage oImage = (AsyncSrcImage)Element;
if (oImage != null && oImage.AsyncImgUrl != "")
{
oImage.Source = ImageSource.FromUri(new Uri(oImage.AsyncImgUrl));
}
}
}
Maybe that helps other folks ;)

Related

Proper way to implement custom Css attribute with Itext and html2Pdf

I'm using Itext 7 and their html2Pdf lib.
Is there a way to implement for example cmyk colors.
.wootWorkingCMYK-color{
color: cmyk( 1 , 0.69 , 0.08 , 0.54);
}
I know the itext core part pretty good, looking for away to use the html2Pdf side. I'm aware of the CssApplierFactory but this seems to be to far up the chain.
Well, of course there is a way of processing custom CSS properties like cmyk colors, but unfortunately the code would be quite bulky and you will need to write quite some code for different cases. I will show how to apply custom color for font, but e.g. for backgrounds, borders or other cases you will need to write separate code in a similar way. Reason behind it is that iText layout structure, although designed with HTML/CSS in mind, is not 100% similar and has some differences you have to code around.
Having that said, if you can fork, build and use your custom version from sources, this is the way I would advice to go. Although it has drawbacks like having to rebase to get updates, the solution would be simpler and more generic. To do that, search for usages of CssUtils.parseRgbaColor in pdfHTML module, and you will find that it is used in BackgroundApplierUtil, BorderStyleApplierUtil, FontStyleApplierUtil, OutlineApplierUtil. There you will find code like
if (!CssConstants.TRANSPARENT.equals(cssColorPropValue)) {
float[] rgbaColor = CssUtils.parseRgbaColor(cssColorPropValue);
Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
float opacity = rgbaColor[3];
transparentColor = new TransparentColor(color, opacity);
} else {
transparentColor = new TransparentColor(ColorConstants.BLACK, 0f);
}
Which I belive you can tweak to process cmyk as well, knowing that you know core part pretty well.
Now, the solution without custom pdfHTML version is to indeed start with implementing ICssApplierFactory, or subclassing default implementation DefaultCssApplierFactory. We are mostly interested in customizing implementation of SpanTagCssApplier and BlockCssApplier, but you can consult with DefaultTagCssApplierMapping to get the full list of appliers and cases they are used in, so that you can decide which of them you want to process in your code.
I will show you how to add support for custom color space for font color in the two main applier classes I mentioned and you can work from there.
private static class CustomCssApplierFactory implements ICssApplierFactory {
private static final ICssApplierFactory DEFAULT_FACTORY = new DefaultCssApplierFactory();
#Override
public ICssApplier getCssApplier(IElementNode tag) {
ICssApplier defaultApplier = DEFAULT_FACTORY.getCssApplier(tag);
if (defaultApplier instanceof SpanTagCssApplier) {
return new CustomSpanTagCssApplier();
} else if (defaultApplier instanceof BlockCssApplier) {
return new CustomBlockCssApplier();
} else {
return defaultApplier;
}
}
}
private static class CustomSpanTagCssApplier extends SpanTagCssApplier {
#Override
protected void applyChildElementStyles(IPropertyContainer element, Map<String, String> css, ProcessorContext context, IStylesContainer stylesContainer) {
super.applyChildElementStyles(element, css, context, stylesContainer);
String color = css.get("color2");
if (color != null) {
color = color.trim();
if (color.startsWith("cmyk")) {
element.setProperty(Property.FONT_COLOR, new TransparentColor(parseCmykColor(color)));
}
}
}
}
private static class CustomBlockCssApplier extends BlockCssApplier {
#Override
public void apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker) {
super.apply(context, stylesContainer, tagWorker);
IPropertyContainer container = tagWorker.getElementResult();
if (container != null) {
String color = stylesContainer.getStyles().get("color2");
if (color != null) {
color = color.trim();
if (color.startsWith("cmyk")) {
container.setProperty(Property.FONT_COLOR, new TransparentColor(parseCmykColor(color)));
}
}
}
}
}
// You might want a safer implementation with better handling of corner cases
private static DeviceCmyk parseCmykColor(String color) {
final String delim = "cmyk(), \t\r\n\f";
StringTokenizer tok = new StringTokenizer(color, delim);
float[] res = new float[]{0, 0, 0, 0};
for (int k = 0; k < 3; ++k) {
if (tok.hasMoreTokens()) {
res[k] = Float.parseFloat(tok.nextToken());
}
}
return new DeviceCmyk(res[0], res[1], res[2], res[3]);
}
Having that custom code, you should configure the ConverterProperties accordingly and pass it to HtmlConverter:
ConverterProperties properties = new ConverterProperties();
properties.setCssApplierFactory(new CustomCssApplierFactory());
HtmlConverter.convertToPdf(..., properties);
You might have noticed that I used color2 instead of color, and this is for a reason. pdfHTML has a mechanism of CSS property validation (as browsers do as well), to discard invalid CSS properties when calculating effective properties for an element. Unfortunately, there is no mechanism of customizing this validation logic currently and of course it treats cmyk colors as invalid declarations at the moment. Thus, if you really want to have custom color property, you will have to preprocess your HTML and replace declarations like color: cmyk... to color2: cmyk.. or whatever the property name you might want to use.
As I mentioned at the start of the answer, my recommendation is to build your own custom version :)

Where to call an Action when user removes an uploaded image in GWTUpload?

I am using GWTUpload , the library is in here https://code.google.com/p/gwtupload/
The Example code at client side found on that website has this structure:
// Attach an image to the pictures viewer
private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
public void onLoad(PreloadedImage image) {
//showImageFlowPanel code solution 1
image.setWidth("75px");
showImageFlowPanel.add(image);
}
};
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == Status.SUCCESS) {
new PreloadedImage(uploader.fileUrl(), showImage);
UploadedInfo info = uploader.getServerInfo();
String headShotImageUrl="http://"+Window.Location.getHost()+"/" +"images/uploaded/"+info.message;
//headShotImage code solution 2
if(!"".equals(headShotImageUrl) && UriUtils.isSafeUri(headShotImageUrl)){
headShotImage.setUrl(UriUtils.fromString(headShotImageUrl));
}
}
}
};
The example uses showImageFlowPanel (solution 1) to store the image but I want to store the image inside headShotImage which take the url after user uploaded image successfully, see the headShotImage (solution 2) code above.
Ok, the headShotImage code work fine but I dont know how to remove it when users remove the image. If I use showImageFlowPanel as in the solution 1 then the program remove the image automatically for me and I do not need to do anything.
So my question is "Where to call an Action when user removes an uploaded image in GWTUpload?"
You have to use setOnCancelUploaderHandler. Take a look to this code took from the demo.
// When the user clicks a cancel button we get an event
uploader.addOnCancelUploadHandler (
new IUploader.OnCancelUploaderHandler() {
public void onCancel(IUploader uploader) {
for (String iname : uploader.getServerMessage().getUploadedFieldNames()) {
// loadedImages is an temporary table where we are adding all uploaded files
// indexed by field name
Widget w = loadedImages.get(iname);
if (w != null) {
w.removeFromParent();
loadedImages.remove(uploader.getInputName());
}
}
}
});

Using play framework and scala how to display image path from database

How to display image on view page in play framework and scala application,
from image path from database? I know how to display image from file on statically , but i have no idea about how to do this. Please anyone have knew the answer please share here.
You can put some methods on your controller just like this.
I have a controller Profile and I added a method getImage(string id) to return the photo of the person from db...
public class Profile extends Controller {
public static void getImage(String id)
{
models.Person person = models.Person .findById(id);
if(person.photo!=null)
{
ByteArrayInputStream input = new ByteArrayInputStream(person.photo);
renderBinary(input);
return;
}
VirtualFile vf = VirtualFile.fromRelativePath("/public/images/no_photo.jpg");
File f = vf.getRealFile();
renderBinary(f);
}
}
on your View,
you can use
<img src=#{Profile.getImage(person.id)}>
Hope this will help you. :)

Eclipse RCP: Set Image in the status line

I am developing an RCP application, I wanted to set the status line. I figured out that I can extend the ActionBarAdvisor class and by overriding the method fillStatusLine() method I can set the status.
private StatusLineContributionItem statusItem;
#Override
protected void fillStatusLine(IStatusLineManager statusLine) {
statusItem = new StatusLineContributionItem("LoggedInStatus");
statusItem.setText("Logged in");
statusLine.add(statusItem);
}
Now, I wish to set image along with it. Is is possible to add image to status line?
You need to override fill(Composite parent) method in your StatusLineContributionItem. There you can add custom components (images, buttons etc. to a status line). For example: http://book.javanb.com/eclipse-rich-client-platform-designing-coding-and-packaging-java-applications-oct-2005/ch17lev1sec7.html
org.eclipsercp.hyperbola/StatusLineContribution
public void fill(Composite parent) {
Label separator = new Label(parent, SWT.SEPARATOR);
label = new CLabel(parent, SWT.SHADOW_NONE);
GC gc = new GC(parent);
gc.setFont(parent.getFont());
FontMetrics fm = gc.getFontMetrics();
Point extent = gc.textExtent(text);
if (widthHint > 0)
widthHint = fm.getAverageCharWidth() * widthHint;
else
widthHint = extent.x;
heightHint = fm.getHeight();
gc.dispose();
StatusLineLayoutData statusLineLayoutData = new StatusLineLayoutData();
statusLineLayoutData.widthHint = widthHint;
statusLineLayoutData.heightHint = heightHint;
label.setLayoutData(statusLineLayoutData);
label.setText(text);
label.setImage(image);
...
}
You chould use the following class: org.eclipse.ui.texteditor.StatusLineContributionItem.class this contains the method setImage(Image image).
It is found in: plugins/org.eclipse.ui.workbench.texteditor_(version).jar of your eclipse installation.
This is class extends: org.eclipse.jface.action.StatusLineContributionItem.class.
Note there are 2 classes named: StatusLineContributionItem.class the other resides in: plugins/org.eclipse.jface_(version).jar and is named: org.eclipse.jface.action.StatusLineContributionItem.class.
This one however does not contain the setImage(Image image) method.
You can then call:
StatusLineManager statusLine = new StatusLineManager();
StatusLineContributionItem i = new StatusLineContributionItem("myid");
i.setText("myText");
i.setImage(SWTResourceManager.getImage(MyClass.class, "config.gif");
...
statusLine.add(i);
...
return statusLine;
If you want complete customization you can use the solution above overriding the fill(Composite composite) method.
Reference:
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Ftexteditor%2FStatusLineContributionItem.html

From Loader to Bitmap AS3 Flex

i'm trying to load images from Facebook Albums to my flex app, this is the code called when i click on a FB image:
private var loaderFB:Loader;
private var immCaricata:Bitmap = new Bitmap();
//Function where i pass the url of the FB image clicked
private function getFBImage(src:String):void{
var request:URLRequest;
loaderFB=new Loader();
request=new URLRequest(src);
loaderFB.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteFBget);
loaderFB.load(request);
}
private function onCompleteFBget(event:Event):void {
var bitmap_dataFB:BitmapData = new BitmapData(640, 480);
bitmap_dataFB.draw(loaderFB.content);
immCaricata = new Bitmap(bitmap_dataFB);
displayOut.addChild(immCaricata);
}
it doesn't work!
If i pass
displayOut.addChild(loaderFB); //instead displayOut.addChild(immCaricata);
It works like a charm!
My problem is that i need to pass loader content to that bitmap (immCaricata) cause i use it in different other functions.
I tried different solution, but probably i do some mistakes somewhere.
Could you please help me? Hope is all clear.
Thank you so much
EDIT:
The problem was not the syntax but the security policy file, i had to add it:
in init function:
Security.loadPolicyFile("http://graph.facebook.com/crossdomain.xml");
Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");
and then where i load the image
loaderFB=new Loader();
request=new URLRequest(src);
loaderFB.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteFBget);
var lc:LoaderContext = new LoaderContext();
lc.checkPolicyFile = true;
loaderFB.load(request,lc);
controlloImm = true;
Hope is clear and hope it helps someone else. :)
private var loaderFB:Loader;
private var immCaricata:Bitmap;
//Function where i pass the url of the FB image clicked
private function getFBImage(src:String):void{
var request:URLRequest;
loaderFB=new Loader();
request=new URLRequest(src);
loaderFB.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteFBget);
loaderFB.load(request);
}
private function onCompleteFBget(event:Event):void {
immCaricata = event.target.content as Bitmap;
displayOut.addChild(immCaricata);
}
Try this if you dont wanna change the loaded image size, hope this will help