调整iOS

时间:2015-09-03 06:16:20

标签: ios ios8

您好我正在开发一款iOS应用。我有一个UIImageView,其图像与之关联。我正在viewDidLoad()方法中更改其维度。

最初,当我更改尺寸时,我可以在视图上调整图像大小。然而,在我根据图像中对象的形状(即摆脱图像中不需要的部分)裁剪图像(使用Photoshop)之后。我的resize方法似乎不起作用,但是我调用相同的方法时图像的大小没有改变。

我用于调整大小的方法如下所示。

-(void)initXYZ{

CGSize size;
CGFloat x,y;

x = 0+myImageView1.frame.size.width;
y = myImageView2.center.y;
size.width = _myImageView2.frame.size.width/2;
size.height = _myImageView2.frame.size.width/2;

UIImage *image = [UIImage imageNamed:@"xyz.png"];
image = [HomeViewController imageWithImage:image scaledToSize:size xCord:x yCord:y];}

实用方法如下:

+(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize xCord:(CGFloat)X yCord:(CGFloat)Y{


UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}

2 个答案:

答案 0 :(得分:0)

Try this ...

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

OR

+ (UIImage*)imageWithImage:(UIImage*)image 
                   scaledToSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();


    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipV = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipV);  

    CGContextDrawImage(context, newRect, imageRef);


    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

答案 1 :(得分:0)

试试这个:

result = db.engine.execute(text("<sql here>").execution_options(autocommit=True))

根据需要调整- (UIImage*)resizeAndStoreImages:(UIImage*)img { UIImage *chosenImage = img; NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0); int resizedImgMaxHeight = 500; int resizedImgMaxWidth = 500; UIImage *resizedImageData; if (chosenImage.size.height > chosenImage.size.width && chosenImage.size.height > resizedImgMaxHeight) { // portrait int width = (chosenImage.size.width / chosenImage.size.height) * resizedImgMaxHeight; CGRect rect = CGRectMake( 0, 0, width, resizedImgMaxHeight); UIGraphicsBeginImageContext(rect.size); [chosenImage drawInRect:rect]; UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)]; pic1 = nil; } else if (chosenImage.size.width > chosenImage.size.height && chosenImage.size.width > resizedImgMaxWidth) { // landscape int height = (chosenImage.size.height / chosenImage.size.width) * resizedImgMaxWidth; CGRect rect = CGRectMake( 0, 0, resizedImgMaxWidth, height); UIGraphicsBeginImageContext(rect.size); [chosenImage drawInRect:rect]; UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)]; pic1 = nil; } else { if (chosenImage.size.height > resizedImgMaxHeight) { int width = (chosenImage.size.width / chosenImage.size.height) * resizedImgMaxHeight; CGRect rect = CGRectMake( 0, 0, width, resizedImgMaxHeight); UIGraphicsBeginImageContext(rect.size); [chosenImage drawInRect:rect]; UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)]; pic1 = nil; } else { resizedImageData = [UIImage imageWithData:imageData]; } } return resizedImageData; } resizedImgMaxHeight