How to get the device’s scale factor?
The device’s scale factor is a property of UIScreen. You can obtain it by: [UIScreen mainScreen].scale; Documentation here.
The device’s scale factor is a property of UIScreen. You can obtain it by: [UIScreen mainScreen].scale; Documentation here.
Swift 5 Solution I wrote Pixel SDK which offers a more powerful solution to this problem, otherwise here is the simplest solution: extension UIImage { func rotate(radians: Float) -> UIImage? { var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: CGFloat(radians))).size // Trim off the extremely small float value to prevent core graphics from rounding it up … Read more
A bit of searching leads me here since I was facing the similar problem. You code works fine. The problem might be raised from your image. Code: //On the top of your swift extension UIImage { func getPixelColor(pos: CGPoint) -> UIColor { let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage)) let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData) let pixelInfo: Int = … Read more
Setting the UIImageView as: myImageView.image = nil is the correct way to clear an UIImageView. Are you loading the image when your calling function returns? Is your UIImageView being declared, and/or used elsewhere in your main function?
UIImage *img = [UIImage imageNamed:@”sample.png”]; NSData *imgData = UIImageJPEGRepresentation(img, 1.0); NSLog(@”Size of Image(bytes):%d”,[imgData length]);
Try this very simple code: I used to detect a wall in my maze game (the only info that I need is the alpha channel, but I included the code to get the other colors for you): – (BOOL)isWallPixel:(UIImage *)image xCoordinate:(int)x yCoordinate:(int)y { CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); const UInt8* data = CFDataGetBytePtr(pixelData); int pixelInfo = … Read more
That is correct, imageNamed: will search your main bundle. Images in your project, even if they are in different groups in your Project Navigator will be in your main bundle and can be accessed directly by name.
You can use self.imageView.contentMode = UIViewContentModeScaleAspectFit; Swift 3: imageView.contentMode = .scaleAspectFit Or UIViewContentModeCenter / .center, or any of the other modes described in the UIView documentation.
I have achieved that programatically with this code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //create a new button let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton //set image for button button.setImage(UIImage(named: “fb.png”), forState: UIControlState.Normal) //add function for button button.addTarget(self, action: “fbButtonPressed”, forControlEvents: UIControlEvents.TouchUpInside) //set frame button.frame = CGRectMake(0, 0, 53, 31) … Read more
CIImage *ciImage = [UIImage imageNamed:@”test.png”].CIImage; UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage]; To fix the case where myUIImage.CIImage returns nil like [UIImageView image], you can instead do [CIImage imageWithCGImage:myUIImage.CGImage] – Dylan Hand Swift version: let ciImage = UIImage(named: “test.png”)!.ciImage let uiImage = UIImage(ciImage: ciImage) To fix the case where myUIImage.ciImage returns nil like you can instead … Read more