How could Image view display photo by URL in SWiftUI?
I have a photo named "0.jpg" in my iPhone, and the URL is "/var/mobile/Containers/Data/Application/EB3DA19A-F7B2-48DD-8681-139C7FCB9090/Documents/Photos".
And my source code is just follows:
var imageSection: some View {
Image(landmark.photoName,bundle: Bundle(url:FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("Photos")))
}
When running, it would get the error:
[SwiftUI] No image named '0.jpg' found in asset catalog for /var/mobile/Containers/Data/Application/E03608D6-24A5-4797-8C16-DDAEB03AB623/Documents/Photos
And I am sure the jpg file is existing, because I use UIImage as parameter and it would display well:
var imageSection: some View {
Image(uiImage: UIImage(named: "/var/mobile/Containers/Data/Application/EB3DA19A-F7B2-48DD-8681-139C7FCB9090/Documents/Photos/0.jpg"))
}
How could I display the image in the Image without UIImage?
Thank you very much!
See documented Image constructor
/// Creates a labeled image usable as content for controls.
///
/// - Parameters:
/// - name: the name of the image resource to lookup, as well as
/// the localization key with which to label the image.
/// - bundle: the bundle to search for the image resource and
/// localization content. If `nil`, uses the main `Bundle`.
/// Defaults to `nil`.
public init(_ name: String, bundle: Bundle? = nil)
This one "name: the name of the image resource to lookup" - not a file name, but image resource name, and all image resources are now in assets catalogs.
For external images there is Image(uiImage:) which you already found - just use it.
Related
I'm using SwiftUI's Link to open Safari from the application. But I have a long text for the link.
What is the problem
For now, the second line of the text always keeps aligned at the center.
What I want
I want to be able to use leading TextAlignment with it.
So I've tried to use multilineTextAlignment but didn't work.
Code
Link("Some long text even very looong even that long text here!", destination: URL(string: "https://www.apple.com/")!)
.multilineTextAlignment(.leading)
Need help.
Solution
My solution was using another signature of the Link itself with multilineTextAlignment.
Link(destination: URL(string: "https://www.apple.com/")!) {
Text("Some long text even very looong even that long text here!")
.multilineTextAlignment(.leading)
}
From Apple Documentation
public struct Link<Label> : View where Label : View {
/// Creates a control, consisting of a URL and a label, used to navigate
/// to the given URL.
///
/// - Parameters:
/// - destination: The URL for the link.
/// - label: A view that describes the destination of URL.
public init(destination: URL, #ViewBuilder label: () -> Label)
Hope will help someone else!
Best
I'm working on the offline downloads section for a video streaming app. Currently, I'm able to download videos and also can see the file path for the downloads as follows:
Button(action: {
let fileManager = FileManager.default
do {
let resourceKeys : [URLResourceKey] = [.creationDateKey, .isDirectoryKey]
let documentsURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let enumerator = FileManager.default.enumerator(at: documentsURL,
includingPropertiesForKeys: resourceKeys,
options: [.skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
print("directoryEnumerator error at \(url): ", error)
return true
})!
for case let fileURL as URL in enumerator {
let resourceValues = try fileURL.resourceValues(forKeys: Set(resourceKeys))
// print(fileURL.path, resourceValues.creationDate!, resourceValues.isDirectory!)
print(fileURL.path)
self.array.append(fileURL.path)
}
} catch {
print(error)
}
}){
Text("Check downloads")
}
//response when button is clicked
/private/var/mobile/Containers/Data/Application/AD90B554-3465-43DD-BAE2-04BC83F850A3/Documents/Bz75srG5nctDCnAWIspM.mp4
/private/var/mobile/Containers/Data/Application/AD90B554-3465-43DD-BAE2-04BC83F850A3/Documents/GWYgrgDVYaowxeGcOjzp.mp4
Since I've renamed the mp4 files using movie ID's (eg: GWYgrgDVYaowxeGcOjzp.mp4) my goal is to use this name to show which videos were downloaded and allow users to play the video. I'm able to do the name processing later on, but how am I able to get the downloaded mp4 files into a view like a VStack? I was thinking of looping through and adding the files into an array but wanted to check if there's a different way. Also, would be great if you could show me how to play a video using the path as the url. I've tried applying suggested methods but ended up with a "NSURLConnection finished with error - code -1002" error. Thank you!
Can I get the result of detecting a picture to go to the next page in AR.js, for example. Since I saw that it is sending object detection messages to the console, but I cannot use the detection result in the code
According to AR.js documentation, the 'markerFound' is 'Fired when a marker in Marker Based, or a picture in Image Tracking, has been found'.
So if this is how you define an image to track:
<a-nft type="nft" imageMarkerName ...
To add listener:
AFRAME.registerComponent(imageMarkerName, {
init: function () {
var marker = this.el;
marker.addEventListener('markerFound', function() {
// The image is found.
});
});
I'm getting a Image from sitecore with glass
[SitecoreField(FieldName = "Icon Image")]
public virtual Image IconImage { get; set; }
Now I need to get my image's item name. Is there any way of doing this without making another call to the db?
Get the item name from the image source as the name in the path is always the item name:
Path.GetFileNameWithoutExtension(IconImage.Src)
I have the following jsfiddle which he an existing image with a button that when clicked will change the image src randomly from an array.
JSFIDDLE: http://jsfiddle.net/GpSbd/7/
Some of these images will be very large, is it possible to reduce or compress the image file down before it does setImage and draws it onto the stage as i will eventually be uploading the images onto our server so want them to be as small as possible
// CHANGE IMAGE FUNCTION
$('#changeImage').on("click", function(){
var newImage = new Image();
var img = layer.get('#Image1')[0];
newImage.onload = function() {
<-- CAN I COMPRESS IT AT THIS STAGE? -->
img.setImage(newImage);
layer.draw();
};
var random = pictures[Math.floor(Math.random()*pictures.length)];
newImage.src = random;
});
You already have the image urls saved in your pictures[] array.
Just save that url (random) on your server and not the image itself.
This cuts your storage size by 20-100 times.
If the server needs to temporarily use the image, the server can use the url to fetch it.