-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve image docs #33
Open
ErikKalkoken
wants to merge
2
commits into
fyne-io:main
Choose a base branch
from
ErikKalkoken:improve-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,22 @@ redirect_from: | |
|
||
A `canvas.Image` represents a scalable image resource in Fyne. | ||
It can be loaded from a resource (as shown in the example), from an | ||
image file, from a URI location containing an image, from an `io.Reader`, or from a Go `image.Image` in memory. | ||
image file, from a URI location containing an image, from an `io.Reader`, | ||
or from a Go `image.Image` in memory. | ||
|
||
The default image fill mode is `canvas.ImageFillStretch` which will | ||
cause it to fill the space specified (through `Resize()` or layout). | ||
Alternatively you could use `canvas.ImageFillContain` to ensure that | ||
the aspect ratio is maintained and the image is within the bounds. | ||
Further to this you can use `canvas.ImageFillOriginal` (as used | ||
in the example here) which ensures that it will also have a minimum size | ||
equal to that of the original image size. | ||
Images can be bitmap based (like PNG and JPEG) or vector based | ||
(such as SVG). Where possible we recommend scalable images as they will | ||
continue to render well as sizes change. | ||
|
||
## Image from a resource | ||
|
||
The most common approach is to create a `canvas.Image` from a `fyne.Resource`. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/app" | ||
"fyne.io/fyne/v2/canvas" | ||
"fyne.io/fyne/v2/theme" | ||
|
@@ -30,21 +32,79 @@ func main() { | |
myApp := app.New() | ||
w := myApp.NewWindow("Image") | ||
|
||
image := canvas.NewImageFromResource(theme.FyneLogo()) | ||
// image := canvas.NewImageFromURI(uri) | ||
// image := canvas.NewImageFromImage(src) | ||
// image := canvas.NewImageFromReader(reader, name) | ||
// image := canvas.NewImageFromFile(fileName) | ||
image.FillMode = canvas.ImageFillOriginal | ||
r := theme.AccountIcon() | ||
image := canvas.NewImageFromResource(r) | ||
w.SetContent(image) | ||
w.Resize(fyne.NewSize(200, 200)) | ||
w.ShowAndRun() | ||
} | ||
``` | ||
|
||
In our example we use `theme.AccountIcon()`, because it is already available in the Fyne library. To use your own images you can bundle them into your project, which will make them available as Fyne resources. For more information please see [Bundling resources]({% link extend/bundle.md %}). | ||
|
||
Please note that we are setting a size for the window. This is necessary, because images normally do not have a minimum size and Fyne would draw a window with zero width and height. For more details please see the section about [Fillmode](#fillmode). | ||
|
||
## Image from a URL | ||
|
||
Images can also be created from URLs with `canvas.NewImageFromURI()`. This will fetch the image during runtime from a public image URL. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this misleading as you can't pass a URL into a URI method. You might find it easier to use |
||
|
||
Compared to the first example, an additional step is required to parse the URL into an URI: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/app" | ||
"fyne.io/fyne/v2/canvas" | ||
"fyne.io/fyne/v2/storage" | ||
) | ||
|
||
func main() { | ||
myApp := app.New() | ||
w := myApp.NewWindow("Image") | ||
|
||
u, _ := storage.ParseURI("https://avatars.githubusercontent.com/u/36045855") | ||
image := canvas.NewImageFromURI(u) | ||
w.SetContent(image) | ||
w.Resize(fyne.NewSize(200, 200)) | ||
w.ShowAndRun() | ||
} | ||
``` | ||
|
||
Images can be bitmap based (like PNG and JPEG) or vector based | ||
(such as SVG). Where possible we recommend scalable images as they will | ||
continue to render well as sizes change. | ||
Please note that is it often better to bundle images with your Fyne app. Your image will be rendered faster and you avoid potential issues related to network access during runtime. | ||
|
||
## Image from other sources | ||
|
||
You can also create an image from other sources: | ||
|
||
- `image.Image`: `canvas.NewImageFromImage()` | ||
- `io.Reader`: `canvas.NewImageFromReader()` | ||
- file: `canvas.NewImageFromFile()` | ||
|
||
For more information please see the API documentation for `canvas.Image`. | ||
|
||
## Fillmode | ||
|
||
You can specify how the images should expand to fill or fit the available space with `Image.Fillmode`. | ||
|
||
The default fill mode is `canvas.ImageFillStretch` which will cause it | ||
to fill the space specified (through `Resize()` or layout). | ||
|
||
Alternatively you could use `canvas.ImageFillContain` to ensure that | ||
the aspect ratio is maintained and the image is within the bounds. | ||
|
||
Please note that with `canvas.ImageFillStretch` and `canvas.ImageFillContain` images | ||
will use the available space of their container, but do not have a minimum size. | ||
This means that layouts that shrink their objects to their minimum size | ||
(e.g. `layout.NewHBoxLayout()`) will shrink images to zero. | ||
In those cases you can set a minimum size yourself with `Image.SetMinSize()`. | ||
|
||
Another available fill mode is `canvas.ImageFillOriginal`. | ||
It ensures that the container grows to the pixel dimensions required to fit the original image. | ||
The aspect of the image will be maintained so, | ||
as with ImageFillContain there may be transparent areas around the image. | ||
|
||
Be careful when using original image sizes as they may not | ||
behave exactly as expected with different user interface scales. | ||
As Fyne allows the entire user interface to scale, a 25px image file | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not zero so this statement is incorrect - the width and height of the image would be 1, in addition a window is normally padded as well. I would consider rewording this to be less specific.