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 に答える