Q
在IOS
1
A
回答
2
您可以從imageView框架獲得此信息,該框架包含圖像內部或圖像自身。既然你沒有說無論您使用的ImageView或不在這裏既是:
越來越尺寸和ImageView的座標:
let x:CGFloat = imageView.frame.origin.x
let y:CGFloat = imageView.frame.origin.y
let width:CGFloat = imageView.frame.size.width
let height:CGFloat = imageView.frame.size.height
從UIImage的(沒有座標)獲得大小:
let width:CGFloat = image.size.width
let height:CGFloat = image.size.height
0
Swift 3 Xcode 8。 它只能在viewDidAppear
功能
@IBOutlet var myImage: UIImageView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let Height = myImage.frame.height
let Width = myImage.frame.width
let xPosition = myImage.frame.minX
let yPosition = myImage.frame.minY
print("Height: \(Height), width: \(Width), xPosition: \(xPosition), yPosition: \(yPosition)")
}
+0
我需要形狀框架而不是圖像視圖 –
+0
我有很多不同形狀的圖像和不同的位置,特別是像這個圖像 –
1
從圖像讀取的像素,並獲得CGRect
之後,從圖像與您的設備的寬度和高度進行比較。
- (CGRect)getRectFromImage:(UIImage*)image
{
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int x = 0;
int y = 0;
int xPos = 0;
int yPos = 0;
int xMax = 0;
int yMax = 0;
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3];
if (alphaByte > 0) {
if (xPos == 0) {
xPos = x;
}
if (yPos == 0) {
yPos = y;
}
if (x < xPos) {
xPos = x;
}
if (y < yPos) {
yPos = y;
}
if (x > xMax) {
xMax = x;
}
if (y > yMax) {
yMax = y;
}
}
}
}
NSLog(@"(%i,%i,%i,%i)", xPos, yPos,xMax-xPos,yMax-yPos);
free(rawData);
return CGRectMake(xPos, yPos, xMax-xPos, yMax-yPos);
}
相關問題
您在客觀C或迅速 –
需要,我需要SWIFT代碼@ Anbu.Karthik –
請故事看看這個鏈接:https://www.toptal.com/machine-learning/real-time-object -detection-using-mser-in-ios – Ryuk