English 中文(简体)
iPhone: 以想要的形状和大小绘制图像的最简单方式
原标题:iPhone:-simplest way to crop image of desired shape and size
  • 时间:2012-05-24 06:46:26
  •  标签:
  • iphone
  • xcode

我试图按照我的愿望 裁剪图像... 用于我的电影机型应用程序... 并用于我的应用程序

i have tried a lot of options but none of tham is...usefull for me i read answers on stack over flow but of no use

plzz 帮我一把

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];    

感谢 谢谢

最佳回答

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

    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();

现在我的形象是你想要的尺寸

问题回答

请在下方找到图像的刻录代码 。

// 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];

如何刻录图像

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

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

但不要忘记在裁剪前先清除图像方向, 否则在裁剪后, 你就会被旋转图像:

-(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;
}

这是使用 Dinesh s 回答来获取图像平方缩图的类

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
}




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签