7

我正在尝试根据我的需要裁剪图像......对于我的电影类型应用程序......并在我的应用程序中使用它

我尝试了很多选项,但没有一个对我有用……我在堆栈溢出时阅读了答案,但没有用

请帮帮我

image = [UIImage imageNamed:@"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];

CGSize size = [image size];

[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];    

谢谢

4

4 回答 4

11

这是将图像调整为所需大小的确切代码

    CGSize itemSize = CGSizeMake(320,480); // give any size you want to give

    UIGraphicsBeginImageContext(itemSize);

    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);

    [myimage drawInRect:imageRect];


    myimage = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();

现在我的图像是你想要的大小

于 2012-05-25T09:45:46.083 回答
3

请找到下面的代码来裁剪图像。

// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// Get size of current image
CGSize size = [image size];

// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];    

// Create rectangle that represents a cropped image  
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4 , 
    (size.width / 2), (size.height / 2));

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];

参考如何裁剪图像

于 2012-05-24T06:56:55.810 回答
2

我认为这是最简单的方法:

CGRect rect = CGRectMake(0.0, 0.0, 320.0, 430.0);
CGImageRef tempImage = CGImageCreateWithImageInRect([originalImage CGImage], rect);
UIImage *newImage = [UIImage imageWithCGImage:tempImage];
CGImageRelease(tempImage);

但不要忘记在裁剪前清除 imageOrientation,否则裁剪后会得到旋转的图像:

-(UIImage *)normalizeImage:(UIImage *)raw {
    if (raw.imageOrientation == UIImageOrientationUp) return raw;
    UIGraphicsBeginImageContextWithOptions(raw.size, NO, raw.scale);
    [raw drawInRect:(CGRect){0, 0, raw.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}
于 2013-05-02T10:42:59.410 回答
0

这是一个使用 Dinesh 的答案来获取图像的方形缩略图的类

class func returnSquareCroppedImage(theImage:UIImage)->UIImage{

    var mySize:CGFloat = 100.0
    var imageRect:CGRect

    if theImage.size.width > theImage.size.height {

        mySize = theImage.size.height
        let xAdjust = 0-((theImage.size.width - theImage.size.height)/2)
        imageRect = CGRectMake(xAdjust, 0, theImage.size.width, theImage.size.height)
    }else{

        mySize = theImage.size.width
        let yAdjust = 0-((theImage.size.height - theImage.size.width)/2)
        imageRect = CGRectMake(0, yAdjust, theImage.size.width, theImage.size.height)
    }

    UIGraphicsBeginImageContext(CGSizeMake(mySize, mySize))

    theImage.drawInRect(imageRect)

    let returnImage = UIGraphicsGetImageFromCurrentImageContext()

    return returnImage
}
于 2014-11-17T10:17:59.647 回答