I am developing a simple app which downloads image from a url and then applying live text interactions to that image.
Here's that piece of code
import SwiftUI
import VisionKit
struct LiveTextInteractionView: View {
var post: Post
@Environment (\.presentationMode) var presentationMode
var body: some View {
NavigationView {
if let postImageURL = post.imageURL {
LiveTextInteraction(imageName: postImageURL)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
self.presentationMode.wrappedValue.dismiss()
} label: {
Text("Cancel")
}
}
}
.interactiveDismissDisabled(true)
}
else {
Text("NO Image")
}
}
}
}
@MainActor
struct LiveTextInteraction: UIViewRepresentable {
var imageName: URL
let imageView = LiveTextImageView()
let interaction = ImageAnalysisInteraction()
let analyzer = ImageAnalyzer()
func makeUIView(context: Context) -> some UIView {
imageView.imageFrom(url: imageName)
/*👆 Downloading from an URL doesn't work ❌*/
imageView.image=UIImage(named: "one.png")
/*👆Loading from assets folder works ✅*/
imageView.addInteraction(interaction)
imageView.contentMode = .scaleAspectFit
return imageView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
Task {
let configuration = ImageAnalyzer.Configuration([.text,.machineReadableCode])
do {
if let image = imageView.image {
let analysis = try await analyzer.analyze(image, configuration: configuration)
interaction.analysis = analysis;
interaction.preferredInteractionTypes = .automatic
}
}
catch {
print("error:\(error)")
}
}
}
}
class LiveTextImageView: UIImageView {
/* Use intrinsicContentSize to change the default image size so that we can change the size in our SwiftUI View */
override var intrinsicContentSize: CGSize {
.zero
}
}
extension UIImageView {
func imageFrom(url:URL){
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url){
if let image = UIImage(data:data){
DispatchQueue.main.async{
self?.image = image
}
}
}
}
}
}
The problem is the image is getting downloaded and displayed but the live text controls are not appearing.
But when I put an image in the assets folder and tried to load it and the live text controls appear.
When loading image from assets folder👇
When downloading the image from URL👇
Post
Replies
Boosts
Views
Activity
I have created a project with CoreData having One to Many Relationship (1 Category to many Items). I am having problems updating the Item's 'done' property in Item's View it's updating only when i go back to Category View and then navigate back to Item View
var body: some View {
List {
ForEach(category!.CitemsArray) { item in
HStack {
Text("\(item.title!)")
.onTapGesture {
item.done.toggle()
do {
try moc.save()
} catch {
print("error saving done value:\(error)")
}
}
Spacer()
if item.done == true {
Image(systemName: "checkmark.circle.fill")
} else {
Image(systemName: "circlebadge")
}
}
}
.onDelete(perform: deleteItems)
}
.sheet(isPresented: $isPresented, content: {
ItemEditView(category: category, item: ItemModel())
})
.navigationBarItems(trailing: Button(action: {
isPresented.toggle()
}, label: {
Image(systemName: "plus")
}))
.navigationBarTitle(category!.name!)
}
I think i am doing something wrong in the .onTapGesture
Thanks in Advance
I am using the "toy_biplane" and "teapot" models in ARView, I was able to augment them but the gestures for scaling, rotation and translation are not working.
Following is my code:
func updateUIView(_ uiView: ARView, context: Context) {
if let model = modelConfirmedForPlacement {
if let modelEntity = model.modelEntity {
modelEntity.generateCollisionShapes(recursive: true)
modelEntity.name = modelConfirmedForPlacement!.modelName
uiView.installGestures([.translation,.rotation,.scale]for: modelEntity)
let anchorEntity = AnchorEntity(plane: .any)
anchorEntity.name = modelEntity.name
anchorEntity.addChild(modelEntity.clone(recursive: true))
uiView.scene.addAnchor(anchorEntity)
} else {
print("DEBUG: Unable to load ModelEntity->\(model.modelName)")
}
DispatchQueue.main.async {
modelConfirmedForPlacement = nil
}
}
}
I am new to SwiftUI so wanted to ask,
What's the difference when we use protocol UIViewRepresentable vs UIViewControllerRepresentable ?
Can somebody explain with example
Thanks in Advance
I am new to iOS ,so wanted to know is there any way to get the real time WiFi or cellular upload and download speeds like the one’s we see in some widgets
I have saved array of custom Objects to plist file using PropertyListEncoder(), I want to know how to delete a item from plist file
Thanks in advance
I am new to swiftui so i was wondering how to share data that we store in coreData to widget in ios 14
What’s the difference if we write
myArray is a object of strict which has only two items An id and name
List(self.myArray, id: \.id) { currentArray in
....
}
And
List {
ForEach(self.myArray, id: \.id) { currentArray in
......
}
}