I'm using the following code to draw a text over PDF page.Everything works fine, but if you process a PDF with internal links eg: a Book with Content links to pages, the resultant PDF file seems to have the links stripped off.Why does this happen?
let data = NSMutableData()
let consumer = CGDataConsumer(data: data as CFMutableData)!
let context = CGContext(consumer: consumer, mediaBox: nil, nil)!
let pdffile=PDFDocument(url: input)
for y in stride(from: 0, to: pagecount, by: 1)
{
let page: PDFPage = pdffile!.page(at: y)!
var mediaBox = page.bounds(for: PDFDisplayBox.mediaBox)
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
let bounds = page.bounds(for: PDFDisplayBox.mediaBox)
let size = bounds.size
page.draw(with: .mediaBox, to: context)
text.draw(in:drawrect,withAttributes:textFontAttributes);
context.endPDFPage()
}
context.closePDF()
try! data!.write(to: GetOutputFileName(inputfile: x), options[.atomicWrite])
What I have tried is saving the annotation of each page applying it before ending the page
for annotation in pageannottaions
{
page.addAnnotation(annotation)
}
This also does not work.Please advice
Post
Replies
Boosts
Views
Activity
I'm using the following code to Resize an NSimage
func resizeinbuilt(withSize targetSize: NSSize) -> NSImage? {
let frame = NSRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
guard let representation = self.bestRepresentation(for: frame, context: nil, hints: nil) else {
return nil
}
let image = NSImage(size: targetSize, flipped: false, drawingHandler: { (_) -> Bool in
return representation.draw(in: frame)
})
return image
}
Strangely the saved image size in pixels is 400x400 even though on debugging I can clearly see the size as the 100x100
This is the save to disk code
func saveimage(face:NSImage,imageURL:String,format:String) -> Bool
{
let cgRef = face.cgImage(forProposedRect: nil, context: nil, hints: nil)
var newRep: NSBitmapImageRep? = nil
if let aRef = cgRef {
newRep = NSBitmapImageRep(cgImage: aRef)
}
newRep?.size = face.size ?? NSSize.zero
switch format {
case ".png":
let filepath=URL(fileURLWithPath: imageURL+".png")
let dataToSave = newRep?.representation(using: NSBitmapImageRep.FileType.png, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
do
{
try dataToSave?.write(to: filepath)
return true
} catch
{
return false
}
case ".jpg":
let filepath=URL(fileURLWithPath: imageURL+".jpg")
let dataToSave = newRep?.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
do
{
try dataToSave?.write(to:filepath)
return true
} catch
{
return false
}
case ".tif":
let filepath=URL(fileURLWithPath: imageURL+".tif")
let dataToSave = newRep?.representation(using: NSBitmapImageRep.FileType.tiff, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
do
{
try dataToSave?.write(to:filepath)
return true
} catch
{
return false
}
case ".bmp":
let filepath=URL(fileURLWithPath: imageURL+".bmp")
let dataToSave = newRep?.representation(using: NSBitmapImageRep.FileType.bmp, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
do
{
try dataToSave?.write(to:filepath)
return true
} catch
{
return false
}
case ".gif":
let filepath=URL(fileURLWithPath: imageURL+".gif")
let dataToSave = newRep?.representation(using: NSBitmapImageRep.FileType.gif, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
do
{
try dataToSave?.write(to:filepath)
return true
} catch
{
return false
}
default:
return true
}
}
Some users are reporting application crash - EXC_BAD_INSTRUCTION (SIGILL). This could not be reproduced on my M1 Mac running macOS Monterey.I'm unable to find the root cause of the issue.Please advice
Error Report
I'm using the following code to encrypt data using AES algorithm with key as system serial and a string as iv
This is the write function
func writeStringToFile()
{
do{
let path = URL(fileURLWithPath: "/Users/Shared")
let folderurl = path.appendingPathComponent("MYPATH")
let systemserial=self.getsystemserial();
let paddedserial = systemserial.padding(toLength: 16, withPad: "X", startingAt: 0)
let aes1 = try AES(key: paddedserial, iv: "mystrongpasscode") // aes128
let sdata = try aes1.encrypt(Array(mystring.utf8))
let writedata1 = Data(bytes: sdata)
try FileManager.default.createDirectory(at: folderurl, withIntermediateDirectories: true)
let filepath = folderurl.appendingPathComponent("MYFILE")
try writedata1.write(to: filepath)
}
catch
{
}
}
This is the read function
func readStringfromFile() -> String
{
do{
let path = URL(fileURLWithPath: "/Users/Shared")
let folderurl = path.appendingPathComponent("MYPATH")
let filepath = folderurl.appendingPathComponent("MYFILE")
let text1 = try Data(contentsOf: filepath)
let systemserial=self.getsystemserial();
let paddedserial = systemserial.padding(toLength: 16, withPad: "X", startingAt: 0)
let aes1 = try AES(key: paddedserial, iv: "mystrongpasscode") // aes128
let decrypted = try aes1.decrypt(Array(text1))
return String(bytes: decrypted, encoding: .utf8)!
}
catch
{
return " "
}
}
The function i use to get the system serial is the following
func getsystemserial() -> String {
guard case let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice") ), platformExpert > 0, let serialNumber = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String else { return "" }; IOObjectRelease(platformExpert);
return serialNumber
}
But the read and written strings are not matching. What am i doing wrong ?
I'm having issues with notarizing the app directly in Xcode.Even though i have acceped all agreements and updated the required banking info,the upload step get stuck at authenticating with Apple Service.So i'm trying to use alttool to notarize my app.
Generated App specific password
Using the following command
But i keep getting the error
my-MacBook-Pro:1.3 my$ xcrun altool --notarize-app --primary-bundle-id "me.myapp" --username me@live.com --password mypass --file "myapp.app"
*** Error: Notarization failed for 'myapp.app'.
*** Error: The operation couldn’t be completed. Is a directory Is a directory (21)
{
"_kCFStreamErrorCodeKey" = 21;
"_kCFStreamErrorDomainKey" = 1;
}
I'm running Xcode 13.1 on macOS Monterey 12.0.1
What i'm trying to achieve is to draw some text on an image and achieve the same scaling(look) on images of varying sizes.ExampleImage 1https://i.stack.imgur.com/SLZaV.pngImage 2https://i.stack.imgur.com/TyUYJ.pngI'm setting a reference image when the NSTextView text is changed by the user,this image will serve as the reference.When the user selects a different image from a listbox,the reference values will be used for font size calculation.override func controlTextDidChange(_ obj: Notification) {
if(obj.object is NSTextField)
{
let txtfield=obj.object as! NSTextField
if(txtfield.identifier?.rawValue=="txt_box")
{
setreferenceimage()
updatepreviewimage()
}
}
} func setreferenceimage()
{
if(textscalemode == 2 && previewimage != nil)
{
referenceimagesize = previewimage.size
let fontAttributes = [NSAttributedStringKey.font: globalfont]
let fontsize = (text as NSString).size(withAttributes: fontAttributes)
referencetextsize=fontsize
referenceimagepath=selectedfilename
isreferenceset = true
panel_refimage.isHidden=false
}
}Now when the user selects a different image i call the draw function which is supposed to scale the text.The font scaling is calculated like this var yratio:CGFloat = max(referenceimagesize!.height, referencetextsize!.height) /
min(referenceimagesize!.height, referencetextsize!.height);
var newheight:CGFloat = 0;
newheight = image.size.height / yratio;
font = NSFont(name:font!.fontName, size: newheight)!
fontAttributes = [NSAttributedStringKey.font: font]
fontsize = (text as NSString).size(withAttributes: fontAttributes)But this does not work .. see the samples belowThe first image is the reference image .. as you can see the second image text does not have the adequate spacing required from the lefthttps://i.stack.imgur.com/v75nM.jpghttps://i.stack.imgur.com/teDS1.jpg
I use the following code to draw a text on an NSImagefunc drawText(image :NSImage) ->NSImage
{
let text = text
let font = NSFont(name:String(combo_font.stringValue), size: 50)
let imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let fontAttributes = [NSAttributedStringKey.font: font]
let fontsize = (text as NSString).size(withAttributes: fontAttributes)
let textRect = CGRect(x: (image.size.width/2-fontsize.width/2), y: image.size.height/2, width: fontsize.width, height: fontsize.height)
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: NSColor.white,
NSAttributedStringKey.paragraphStyle: textStyle
]
let im:NSImage = NSImage(size: image.size)
let rep:NSBitmapImageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(image.size.width), pixelsHigh: Int(image.size.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSColorSpaceName.calibratedRGB, bytesPerRow: 0, bitsPerPixel: 0)!
im.addRepresentation(rep)
im.lockFocus()
image.draw(in: imageRect)
text.draw(in: textRect, withAttributes: textFontAttributes)
im.unlockFocus()
return im
}To prevent UI Freezing i do the long running operation in background threadoverride func controlTextDidChange(_ obj: Notification) {
if(obj.object is NSTextField)
{
let textdata=obj.object as! NSTextField
if(txtfield.identifier?.rawValue=="txt_field")
{
self.textdata=self.txtbox.stringValue
DispatchQueue.global().async {
self.img_view.image=self.drawText(image: NSImage(byReferencingFile: self.selectedfilename)!);
}
}
}
}When processing an Image of 391KB in size, the process takes too long to update the UI.How can i improve the performance.I just need to display the preview to the user,resizing image to a smaller size is also an option to improve the performance;But the same look should be achieved in full resolution image as well when processing later.
My product does not have any inapp purchases.All i want to check is that if the user has purchased the app from the app store as i cant implement my own licensing scheme. I have seen many examples on receipt validation to achieve the same,All these tutorials are based on inapp purchases. Does apple provide a receipt when the user purchases the app from the app store? If yes,how can i sandbox test it?
I have an app that I intend to distribute via the App store. Currently, the user purchases a license from the website, he receives a key via email and our web server validates the license key entered by the user in the application. Is it possible to distribute the app in the App Store without any change ie:the user purchase on app store,he receives a key.Can i distribute the trial version as free on App store?